Compare commits

...

7 Commits

  1. 71
      .vscode/tasks.json
  2. 2
      basic/1_printf.c
  3. 16
      basic/2_datatype.c
  4. 16
      basic/3_pointer.c
  5. 32
      basic/4_memoryManagement.c
  6. 14
      basic/5_function.c
  7. 19
      basic/6_struct.c
  8. 19
      sampleProject/studentGrade/main.c
  9. 40
      sampleProject/studentGrade/student.c
  10. 20
      sampleProject/studentGrade/student.h

71
.vscode/tasks.json vendored

@ -1,5 +1,71 @@
{
"version": "2.0.0",
"runner": "terminal",
"type": "shell",
"echoCommand": true,
"presentation": {
"reveal": "always"
},
"tasks": [
{
"label": "save and compile for C++",
"command": "g++",
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}"
],
"group": "build",
"problemMatcher": {
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "save and compile for C",
"command": "gcc",
"args": [
"-g",
"${fileDirname}/*.c",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}"
],
"group": "build",
"problemMatcher": {
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "execute",
"command": "cmd",
"group": "test",
"args": [
"/C",
"${fileDirname}\\bin\\${fileBasenameNoExtension}"
]
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 활성 파일 빌드",
@ -7,7 +73,7 @@
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"${fileDirname}\\*.c",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
],
@ -23,6 +89,5 @@
},
"detail": "디버거에서 생성된 작업입니다."
}
],
"version": "2.0.0"
]
}

@ -12,7 +12,7 @@ int main()
printf("Price: %f\n", year); // f: 실수 (소수점 아래 6자리)
printf("Price2: %.2f\n", year); // .2f: 소수점 아래 두 자리 실수
printf("Grade: %c\n", grade); // c: 문자
printf("Message: %s\n", message); // x: 16진수
printf("Message: %s\n", message); // s: 문자열
printf("255 to Hex: %x\n", hexNum); // x: 16진수
return 0;

@ -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…
Cancel
Save