C/C

모듈화 2 - 구조체

HicKee 2022. 10. 8. 23:27

st_file_01.c

#include "def.h" //def.h 는 보통 설정 정보를 넣는 다. 
//def.h 가장 위쪽으로
//" " 의미는 st_file_01.c 와 같은 폴더내에 위치 한다라는 뜻이다.
#include <stdio.h> //내부적으로 경로가 연결 (패스가 걸려있다.)
#include "student.h"

int main() {

	ST st1 = { "홍길동",100,90,80,0,0.0,'A' }; //이러한 사용방법도 존재한다.
	init_student();
	show_student(st1);

}

student.c

#include "def.h"
#include <string.h>
#include <stdio.h>
#include "student.h"
//함수
//학생 초기 정보
void init_student() {

	//문자열 복사
	strcpy(st.name, "이길동");
	st.kor = 100;
	st.eng = 100;
	st.math = 100;
	st.hap = st.kor + st.eng + st.math;
	st.avg = st.hap / 3;

	printf("=====================\n");
	printf("학생 기본 정보 목록\n");
	printf("=====================\n");
	printf("학생정보 :%s\n",st.name);
	printf("국어점수 :%d\n", st.kor);
	printf("영어점수 :%d\n", st.eng);
	printf("수학점수 :%d\n", st.math);
	printf("총합 :%d\n", st.hap);
	printf("평균 :%.2f\n", st.avg);
}

void show_student(ST st) {

	printf("=====================\n");
	printf("추가 학생 정보 목록\n");
	printf("=====================\n");
	printf("학생정보 :%s\n", st.name);
	printf("국어점수 :%d\n", st.kor);
	printf("영어점수 :%d\n", st.eng);
	printf("수학점수 :%d\n", st.math);
	printf("총합 :%d\n", st.hap);
	printf("평균 :%.2f\n", st.avg);

}

student.h

#ifndef  __STUDENT_H__
#define  __STUDENT_H__

typedef struct _STUDENT {
	//_STUDENT ST 같으면 안된다.

	char name[10]; //이름
	int kor;
	int eng;
	int math;
	int hap;
	double avg;
	char grade;

} ST;

ST st; //구조체 변수 선언 (전역 변수)

extern void init_student(); //함수 프로토 타입
void show_student(ST st); //extern 생략 가능

#endif

def.h

#ifndef __DEF_H__ // 정의되지 않으면 if not define
#define __DEF_H__

#define _CRT_SECURE_NO_WARNINGS

#endif

 

'C > C' 카테고리의 다른 글

time(), localtime() 시간  (0) 2022.10.13
파일 입출력  (0) 2022.10.11
모듈화 1  (0) 2022.10.08
구조체 1  (0) 2022.10.07
배열 2 - 출력  (0) 2022.10.06