[C/C++] 전처리기

HwangBBang·2023년 1월 18일
0

본 포스팅은 [C/C++] 비트 연산자과 연관되어있습니다. [C/C++] 비트 연산자를 보지 않으셨다면 보고오시는것을 추천드립니다.

전처리기란?

#이 붙은 구문은 모든 컴파일 과정 중 가장 먼저 처리된다. 이 과정을 전처리기 라고한다.

define 전처리기

<전처리기 전>
	#define STARVE 0x0
    #define NORMAL 0x1
    #define FULL 0x2
    
    int main(void)
    {
     unsigned int state = 0x0;
     state = state | STARVE;
     state = state | NORMAL;
     state = state | FULL;
     
      if (state & STARVE){
          cout << "i'm hungry";
          }
      else if (state & NORMAL){
          cout << "i'm good";
          }
      else if (state & FULL){
          cout << "i'm full";
          }
      else{
          cout << "i don't know";
          }
      int state = NORMAL;          
      int state = FULL;
      
    }

전처리 과정을 먼저 거친 후 코드가 진행된다.

<전처리기 후>
	#define STARVE 0x0
    #define NORMAL 0x1
	#define FULL 0x2
    
    int main(void)
    {
    
     unsigned int state = 0x0;
     
     state = state | STARVE;
     state = state | NORMAL;
     state = state | FULL;
     
      if (state & 0x0){
          cout << "i'm hungry";
          }
      else if (state & 0x1){
          cout << "i'm good";
          }
      else if (state & 0x2){
          cout << "i'm full";
          }
      else{
          cout << "i don't know";
          }
          
      int state = 2;
      
    }

전처리기를 왜 사용할까?

define 전처리기를 사용하면?
-> 가독성 을 얻을 수 있다.
-> 유지 보수 에 용이하다.

오류나 질문에 대한 문의 댓글 혹은 메일로 남겨주세요!

profile
https://hwangbbang.tistory.com/

0개의 댓글