What exactly does an #if 0 … #endif block do?

后端 未结 9 1429
眼角桃花
眼角桃花 2020-12-02 04:38

In C/C++

What happens to code placed between an #if 0/#endif block?

#if 0

//Code goes here

#endif


        
相关标签:
9条回答
  • 2020-12-02 05:01

    I'd like to add on for the #else case:

    #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
    
    0 讨论(0)
  • 2020-12-02 05:03

    It permanently comments out that code so the compiler will never compile it.

    The coder can later change the #ifdef to have that code compile in the program if he wants to.

    It's exactly like the code doesn't exist.

    0 讨论(0)
  • 2020-12-02 05:07

    Not only does it not get executed, it doesn't even get compiled.

    #if is a preprocessor command, which gets evaluated before the actual compilation step. The code inside that block doesn't appear in the compiled binary.

    It's often used for temporarily removing segments of code with the intention of turning them back on later.

    0 讨论(0)
  • 2020-12-02 05:10

    When the preprocessor sees #if it checks whether the next token has a non-zero value. If it does, it keeps the code around for the compiler. If it doesn't, it gets rid of that code so the compiler never sees it.

    If someone says #if 0 they are effectively commenting out the code so it will never be compiled. You can think of this the same as if they had put /* ... */ around it. It's not quite the same, but it has the same effect.

    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.

    0 讨论(0)
  • 2020-12-02 05:11

    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.

    0 讨论(0)
  • 2020-12-02 05:12

    What exactly does an #if 0 … #endif block do?

    It tells you that the author has obviously never heard of a version control system. Which, in turn, tells you to run as far away as possible …

    0 讨论(0)
提交回复
热议问题