Compare commits
7 Commits
810bb51a3a
...
d38063dd9d
Author | SHA1 | Date |
---|---|---|
|
d38063dd9d | 6 days ago |
|
b65491612f | 6 days ago |
|
3681cffbb5 | 6 days ago |
|
3ae8f399af | 6 days ago |
|
c46ae60780 | 6 days ago |
|
e93647efdf | 6 days ago |
|
66bcd2a9d8 | 6 days ago |
@ -0,0 +1,16 @@ |
||||
#include <stdio.h> |
||||
|
||||
int main() |
||||
{ |
||||
int age = 30; |
||||
float temperature = 36.6; |
||||
char grade = 'A'; |
||||
double pi = 3.1415926538; |
||||
|
||||
printf("Age: %d\n", age); |
||||
printf("Temperature: %.1f\n", temperature); |
||||
printf("Grade: %c\n", grade); |
||||
printf("Pi: %.10f\n", pi); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,16 @@ |
||||
#include <stdio.h> |
||||
|
||||
int main() |
||||
{ |
||||
int a = 10; |
||||
int *p = &a; // p는 a의 주소를 저장
|
||||
// &: 변수의 주소
|
||||
// *: 주소에 저장된 값
|
||||
|
||||
printf("Value of a: %d\n", a); |
||||
printf("Address of a: %p\n", (void*)&a); // void*: 일반 포인터
|
||||
printf("Address of a: %x\n", p); |
||||
printf("Pointing value of p: %d\n", *p); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,32 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
|
||||
int main() |
||||
{ |
||||
const int COUNT = 5; |
||||
|
||||
// 동적 메모리 할당, ptr에 동적으로 할당된 메모리의 시작주소 저장
|
||||
int *ptr = (int*)malloc(sizeof(int) * COUNT); |
||||
if (!ptr) |
||||
{ |
||||
printf("Fail to malloc\n"); |
||||
return 1; |
||||
} |
||||
|
||||
for (int i = 0; i < COUNT; i++) |
||||
{ |
||||
ptr[i] = i * 10; // 값 초기화
|
||||
printf("%d ", ptr[i]); // 값 출력
|
||||
} |
||||
printf("\n"); |
||||
|
||||
free(ptr); |
||||
|
||||
printf("\n"); |
||||
for (int i = 0; i < COUNT; i++) |
||||
{ |
||||
printf("%d ", ptr[i]); // 값 출력
|
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,14 @@ |
||||
#include <stdio.h> |
||||
|
||||
int add(int a, int b) |
||||
{ |
||||
return a + b; |
||||
} |
||||
|
||||
int main() |
||||
{ |
||||
int sum = add(5, 3); |
||||
printf("Sum: %d\n", sum); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,19 @@ |
||||
#include <stdio.h> |
||||
|
||||
struct Person |
||||
{ |
||||
char name[50]; |
||||
int age; |
||||
float height; |
||||
}; |
||||
|
||||
int main() |
||||
{ |
||||
struct Person person = { "Alice", 30, 170.5 }; |
||||
|
||||
printf("Name: %s\n", person.name); |
||||
printf("Age: %d\n", person.age); |
||||
printf("Height: %.2f\n", person.height); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,19 @@ |
||||
#include <stdio.h> |
||||
#include "student.h" |
||||
|
||||
int main() |
||||
{ |
||||
Student* student1 = createStudent("Bill Gates", 1001, 95.5); |
||||
Student* student2 = createStudent("Steve Jobs ", 1002, 88.0); |
||||
|
||||
printStudentInfo(student1); |
||||
printStudentInfo(student2); |
||||
|
||||
freeStudent(student1); |
||||
freeStudent(student2); |
||||
|
||||
printStudentInfo(student1); |
||||
printStudentInfo(student2); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,40 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include "student.h" |
||||
|
||||
// 학생생성 함수
|
||||
Student* createStudent(const char* name, int id, float grade) |
||||
{ |
||||
Student* newStudent = (Student*)malloc(sizeof(Student)); |
||||
if (!newStudent) |
||||
{ |
||||
printf("Fail to student malloc!\n"); |
||||
exit(1); |
||||
} |
||||
|
||||
strncpy(newStudent->name, name, sizeof(newStudent->name) - 1); // 가용한 마지막 문자까지 복사하고
|
||||
newStudent->name[sizeof(newStudent->name) - 1] = '\0'; // 마지막\0 null문자 추가로 문자열 종료 보장
|
||||
newStudent->id = id; |
||||
newStudent->grade = grade; |
||||
|
||||
return newStudent; |
||||
} |
||||
|
||||
// 학생정보 출력 함수
|
||||
void printStudentInfo(const Student* student) |
||||
{ |
||||
if (!student) |
||||
{ |
||||
printf("Invalid student info."); |
||||
return; |
||||
} |
||||
|
||||
printf("Name: %s, ID: %d, Grade: %.2f\n", student->name, student->id, student->grade); |
||||
} |
||||
|
||||
void freeStudent(Student* student) |
||||
{ |
||||
if (student) |
||||
free(student); |
||||
} |
@ -0,0 +1,20 @@ |
||||
#ifndef STUDENT_H |
||||
#define STUDENT_H |
||||
|
||||
typedef struct |
||||
{ |
||||
char name[50]; |
||||
int id; |
||||
float grade; |
||||
} Student; |
||||
|
||||
// 학생 생성 함수
|
||||
Student* createStudent(const char* name, int id, float grade); |
||||
|
||||
// 학생정보 출력 함수
|
||||
void printStudentInfo(const Student* student); |
||||
|
||||
// 학생 메모리 해제 함수
|
||||
void freeStudent(Student* Student); |
||||
|
||||
#endif |
Loading…
Reference in new issue