C언어 fwrite() fread()로 enum, struct 읽고 쓰기

#include <stdio.h> #include <errno.h> #include <string.h> typedef enum _product_size {SIZE_S = 1 , SIZE_M, SIZE_L, SIZE_XL, SIZE_XXL} PRODUCT_SIZE; typedef enum _product_type {TYPE_OUTER= 1 , TYPE_TOP, TYPE_BOTTOM} PRODUCT_TYPE; typedef struct _cloth { char product_name[ 64 ]; unsigned product_price; char brand_name[ 64 ]; PRODUCT_TYPE product_type; PRODUCT_SIZE product_size; } cloth; void printPS (PRODUCT_SIZE ps); void printPT (PRODUCT_TYPE pt); int main ( int argc, char const *argv[]) { /* code */ FILE *outfile = NULL ; FILE *infile = NULL ; int i = 0 ; int num_cloth = 0 ; cloth temp_product = { 0 }; printf ( "How many products are here?" ); scanf ( "%d" , &num_cloth); if (num_cloth > 0 ){ outfile = fopen ( "C: \\ temp \\ cloth.dat" , "w" ); if ( NULL != outfile){ ...

C언어 Union과 Enum

#include <stdio.h> typedef enum _GradeType { TYPE_GRADE, TYPE_SCORE} GradeType; typedef struct _my_score { GradeType type; union { char grade; int score; } result; } my_score; int main () { int grade_type = TYPE_GRADE; my_score s1 = { 0 }; printf ( "GradeType input : ( 0 : Grade, 1 : Score)?" ); scanf ( "%d" , &grade_type); switch (grade_type){ case TYPE_GRADE: { char temp[ 10 ] = { 0 }; printf ( "input Grade ( A ~ F )" ); scanf ( "%s" , temp); s1. result . grade = temp[ 0 ]; break ; } case TYPE_SCORE: printf ( "input Score ( 0 ~ 100 )" ); scanf ( "%d" , &s1. result . score ); break ; default : printf ( "Wrong Type" ); ...

C 언어 함수에 구조체 변수 전달과 반환

#include <stdio.h> #include <stdlib.h> #include <string.h> struct s_student { char name[ 20 ]; int age; long number; }; void print_student_info ( struct s_student student); void input_student_info ( struct s_student *pStudent); int main ( int argc, char const *argv[]) { /* code */ struct s_student student = { "NAM" , 28 , 60121315 }; printf ( "Initial Value \n " ); print_student_info (student); input_student_info (&student); printf ( "After Calling Function input_student_inf() \n " ); print_student_info (student); return 0 ; } void print_student_info ( struct s_student student) { printf ( "Name : %s \t Age : %d \t Number : %ld \n " , student. name , student. age , student. number ); } void input_student_info ( struct s_student * pStudent) { if ( NULL != pStudent) { printf ( "Student Name ? " ); scanf ( "%s...

Visual Studio Code Window에서 C/C++ 빌드및 실행

이미지
비주얼 스튜디오 코드는 이미 설치가 끝났다는 전제하에 그리고 한글팩을 이미 설치했다는 전제하에서 진행해보겠습니다. C/C++을 지원해주는 확장팩을 깔아줍니다. 그리고 윈도우용 gcc, g++ 컴파일러를 설치해줍니다. 아래 사진참고 Installer를 실행한 다음, 아래사진과 같이 초록색으로 마킹 되어 있는 것들을 체크후 설치 해줍니다. 아래사진은 이미 설치하고 나서의 화면입니다. 그리고 환경변수 Path에 MinGW 설치경로를 잡아줍니다. 설치 후 cmd에 확인! 아래의 간단한 코드를 빌드하고 실행해보겠다. 아래와 같이 터미널탭에서 빌드 작업 실행을 누른다. 아래와 같은 화면이 뜨는데 오른쪽 빌드 작업 구성... 을 클릭한다. 아래처럼 바뀌는데 Others를 다시 클릭한다. 그리고 아래처럼 바뀌면 다시 클릭 그러면 아래와 같이 tasks.json 파일이 생성된다. 아래 처럼 코드를 바꾼다. 두가지 task가 있는데 build_c, exec_c 이다. 직관적인 이름 그자체로 build_c는 c언어로 짜여진 소스를 컴파일 해주는 작업이고 exec_c는 컴파일된 파일을 실행시켜주는 작업이다. 이제 아까 했던 터미널탭에서 빌드 작업을 실행 하고, 그다음 작업 실행을 누르고 exec_c를 선택하면 아래처럼 빌드 하고 실행한다. 혹시 더 괜찮은 방법이 있으면 댓글로 알려주세요~ 아래 블로그를 참고해서 제가 직접 실습한 것을 블로깅했습니다. http://webnautes.tistory.com/1158 그럼 이만 허접의 기록입니다.

C 언어 자기 참조 구조체

#include <stdio.h> #include <stdlib.h> struct linked_list {         char name[20];         int age;         struct linked_list *left_link;         struct linked_list *right_link; }; int main() {         struct linked_list first_person =  {"남동길", 28, NULL, NULL};         struct linked_list second_person = {"아무개", 30, NULL, NULL};         struct linked_list third_person = {"박서방", 35, NULL, NULL};         first_person.right_link = &second_person;         second_person.left_link = &first_person;         second_person.right_link = &third_person;         third_person.left_link = &second_person;         printf("첫번째 사람 : %s\t%d\n",first_person.name, first_person.age);         prin...

C 언어 구조체 배열의 동적 할당

포인터변수를 선언하고 malloc()함수를 사용해서 메모리를 할당하고 free()함수로 해제 #include <stdio.h> #include <stdlib.h> struct s_teacher {         char name[20];         int age;         long number; }; struct s_student {         char name[20];         int age;         long number;         struct s_teacher* pTeacher; //포인터 변수 }; void inputCounts(int *sCnt, int *tCnt); int main() {         int sCnt =0, tCnt =0;         inputCounts(&sCnt, &tCnt);         if(sCnt <= 0 || tCnt <= 0)         {                 printf("선생과 학생수는 1명 이상이여야 됩니다.\n");                 return 0;         }         printf("선생 수 : %d, \t 학생 수  : %d\n", ...

C 언어 구조체의 포인터 멤버 변수

학생11, 학생21은 선생1을 담임선생으로, 학생21, 학생22는 선생2를 담임선생으로 두고 있는 코드이다. #include <stdio.h> struct s_teacher { char name[20]; int age; long number; }; struct s_student { char name[20]; int age; long number; struct s_teacher* pTeacher; //포인터 변수 }; int main() { struct s_student student11 = {"Lee", 20, 4567}; struct s_student student12 = {"Park", 21, 4568}; struct s_student student21 = {"Choi", 20, 4567}; struct s_student student22 = {"Han", 21, 4568}; struct s_teacher teacher1 = {"Kim", 35, 1234}; struct s_teacher teacher2 = {"Nam", 28, 1235}; student11.pTeacher = &teacher1; student12.pTeacher = &teacher1; student21.pTeacher = &teacher2; student22.pTeacher = &teacher2; printf("포인터를 이용한 구조체 변수 접근\n"); printf("학생 %s의 담임교사 이름 : %s\n", student11.name,student11.pTeacher->name); printf("학생 %s의 담임교사 교번 : %ld\n",student12.name,student...