#include "count.h"
int add(int a, int b) {
count++;
return a + b;
}
#ifndef _COUNT_H_
#define _COUNT_H_ 20
int count = 0;
#endif
#include "count.h"
int minus(int a, int b) {
count++;
return a - b;
}
/*------------------------------------------------------------------------------------------
@ Pratice C laguage
Author : SAM
Created : 30-03-2017
Modified : 30-03-2017
Language/ver : C in MSVS2015
Description : Practice
KEYWORD : #ifndef, #endif
------------------------------------------------------------------------------------------*/
#include<stdio.h>
#include "minus.h"
#include "add.h"
int main() {
int result1;
int result2;
result1 = add(3, 4);
result2 = minus(4, 3);
printf("add : %d\n", result1);
printf("monus : %d\n", result2);
printf("count : %d\n", count);
return 0;
}
1. 메인파일에서 add.h, minus.h 를 포함한다. 그에 따라 count.h 가 중복으로 포함되게 된다. 이 경우를 해결하기 위해서
2. count.h 파일에 #ifnedf 와 #endif 를 넣어서 중복을 확인해준다.
3. 이곳에서 지정해준 #define _COUNT_H_ 20 의 20은 의미 없는 값이다.
4. COUNT_H_ 는 헤더파일 이름을 빌려온 것이다. 중복되지 않는 것을 넣는 것이 목적이기에 파일명은 중복되지 않음을 이용한다.
5. 그러니 컴파일 도중에 먼저 헤더파일을 불러왔는지 안왔는지 확인하기 위한 용도이다.
정리를 하자면, 헤더파일 중복을 막기 위해서 사용하는 것이고, #ifndef #endif 를 활용한다. 그러면 중복안 됨, 정의가 안되어 있으면 정의하고, 정의가 되어 있으면 넘기고. 오케이?