1. 참이면 출력하기 #if #endif

#include <stdio.h>
#define a 0 // a는 0이다.
int main(){
#if a // if(0)과 같다.
printf("1번");
#else
printf("2번");
#endif
return 0;
}

// if else문과 비슷하다. a가 참이 아니므로 else에 있는 "2번"을 출력함
// 답 - 2번

2. 참이면 출력하기 #if #elif #endif

#include <stdio.h>
#define a 4
int main(){
#if a == 1 // if(a==1) 인가? 아니므로 다음순서로..
printf("1번");
#elif a ==2
printf("2번");
#elif a ==3
printf("3번");
#elif a ==4
printf("4번");
#endif
return 0;
}

// elif는 else if와 같다.
// 답 - 4번

3. 존재하면 출력하기 ifdef
#include <stdio.h>
#define a 0
int main(){
#ifdef a // define으로 a를 선언했는가?
printf("1번");
#else
printf("2번");
#endif
return 0;
}

// ifdef는 값에 상관없이 선언만 되어있으면 출력한다
// 답 - 1번

4. 존재하지 않으면 출력하기 ifndef

#include <stdio.h>
#define b 1
int main(){
#ifndef a
printf("1번");
#endif
#ifndef b
printf("2번");
#endif
return 0;
}

// define으로 a는 선언되지 않았으므로 a안에 있는 printf를 출력한다.
// b는 선언되어있으므로 출력하지 않는다.
// 답 - 1번

'C' 카테고리의 다른 글

[C] 2차원 배열의 표현  (0) 2019.12.04
[C] 변수형 문자열 상수형 문자열  (0) 2019.12.03
[C] 파일 위치지시자 fseek  (0) 2019.12.03
[C] 파일입출력 fprintf fscanf  (0) 2019.12.03
[C] 파일입출력 fgetc fgets  (0) 2019.12.03
블로그 이미지

ryancha9

https://blog.naver.com/7246lsy

,