Programing

#if 0… #endif 블록은 정확히 무엇을합니까?

lottogame 2020. 7. 21. 20:48
반응형

#if 0… #endif 블록은 정확히 무엇을합니까?


에서 C / C ++

#if 0/ #endif블록 사이에 배치 된 코드는 어떻게됩니까 ?

#if 0

//Code goes here

#endif

코드가 단순히 건너 뛰어 실행되지 않습니까?


실행되지 않을뿐만 아니라 컴파일되지도 않습니다.

#if전 처리기 명령으로 실제 컴파일 단계 전에 평가됩니다. 해당 블록 내부의 코드는 컴파일 된 바이너리에 나타나지 않습니다.

나중에 다시 켜려는 의도로 코드 세그먼트를 일시적으로 제거하는 데 사용됩니다.


하나의 중요한 차이점을 제외하고는 블록을 주석 처리하는 것과 동일합니다. 중첩은 문제가 아닙니다. 이 코드를 고려하십시오.

foo();
bar(x, y); /* x must not be NULL */
baz();

의견을 남기고 싶다면 다음을 시도해보십시오.

/*
foo();
bar(x, y); /* x must not be NULL */
baz();
*/

Bzzt. 구문 오류! 왜? 블록 코멘트 (당신이 SO의 구문 강조에서 볼 수있는 바와 같이) 그렇게하지 둥지 등을 할 수 있기 때문에 */단어 "NULL"후가 만드는 코멘트를 종료 baz주석하지 전화를하고, */이후에 baz구문 오류입니다. 반면에 :

#if 0
foo();
bar(x, y); /* x must not be NULL */
baz();
#endif

전체 내용을 주석 처리합니다. 그리고 #if 0s는 다음과 같이 서로 중첩됩니다.

#if 0
pre_foo();
#if 0
foo();
bar(x, y); /* x must not be NULL */
baz();
#endif
quux();
#endif

물론 이것은 제대로 언급되지 않으면 약간 혼란스럽고 유지 보수 두통이 될 수 있습니다.


컴파일러가 코드를 컴파일하지 않도록 해당 코드를 영구적으로 주석 처리합니다.

코더는 나중에 #ifdef를 변경하여 원하는 경우 프로그램에서 해당 코드를 컴파일 할 수 있습니다.

코드가 존재하지 않는 것과 같습니다.


#if 0… #endif 블록은 정확히 무엇을합니까?

그것은 저자가 분명히 버전 제어 시스템에 대해 들어 본 적이 없다는 것을 알려줍니다. 그 결과 가능한 한 멀리 도망 가라고합니다.


#else경우 에 추가하고 싶습니다 .

#if 0
   /* Code here will NOT be complied. */
#else
   /* Code will be compiled. */
#endif


#if 1
   /* Code will be complied. */
#else
   /* Code will NOT be compiled. */
#endif

전처리기에 #if가 표시되면 다음 토큰에 0이 아닌 값이 있는지 확인합니다. 그렇다면 컴파일러의 코드를 유지합니다. 그렇지 않은 경우 해당 코드를 제거하므로 컴파일러는이를 볼 수 없습니다.

누군가 #if 0이라고 말하면 코드를 효과적으로 주석 처리하므로 컴파일되지 않습니다. 마치 그들이 주변에 / * ... * /를 넣은 것처럼 동일하게 생각할 수 있습니다. 똑같지는 않지만 같은 효과가 있습니다.

If you want to understand what happened in detail, you can often look. Many compilers will allow you to see the files after the preprocessor has run. For example, on Visual C++ the switch /P command will execute the preprocessor and put the results in a .i file.


Lines beginning with a # are preprocessor directives. #if 0 [...] #endif blocks do not make it to the compiler and will generate no machine code.

You can demonstrate what happens with the preprocessor with a source file ifdef.cxx:

#if 0
This code will not be compiled
#else
int i = 0;
#endif

Running gcc -E ifdef.cxx will show you what gets compiled.

You may choose to use this mechanism to prevent a block of code being compiled during the development cycle, but you would probably not want to check it in to your source control as it just adds cruft to your code and reduces readability. If it's a historical piece of code that has been commented out, then it should be removed: source control contains the history, right?

Also, the answer may be the same for both C and C++ but there is no language called C/C++ and it's not a good habit to refer to such a language.


Not quite

int main(void)
{
   #if 0
     the apostrophe ' causes a warning
   #endif
   return 0;
}

It shows "t.c:4:19: warning: missing terminating ' character" with gcc 4.2.4


It is a cheap way to comment out, but I suspect that it could have debugging potential. For example, let's suppose you have a build that output values to a file. You might not want that in a final version so you can use the #if 0... #endif.

Also, I suspect a better way of doing it for debug purpose would be to do:

#ifdef DEBUG
// output to file
#endif

You can do something like that and it might make more sense and all you have to do is define DEBUG to see the results.

참고URL : https://stackoverflow.com/questions/2852913/what-exactly-does-an-if-0-endif-block-do

반응형