Why would somebody use an #if 1 C preprocessor directive?

前端 未结 9 2028
谎友^
谎友^ 2020-12-10 01:42

I am looking through some C source code and I don\'t understand the following part

#if 1

   typedef u         


        
相关标签:
9条回答
  • 2020-12-10 02:18

    Yes.. Only the first block will be processed --- until someone changes the 1 to a 0. Then the other block will be compiled. This is a convenient way to temporary switch blocks of code in and out while testing different algorithms.

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

    It is just a different way to comment out big piece of code, so, editor auto indentation would not break indentation (commented block of code would be indented as text, not as code).

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

    So that one can quickly choose which part to compile by changing the #if 1 to #if 0.

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

    I put that in my code when I need to test different set of parameters. Usually my product will ship with different defaults than what I can work with in a debug environment, so I put the shipping defaults in a #if 1 and the debug defaults in the #else with a #warning to warn me it's being built with debug defaults.

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

    I'm actually using it as a kludge to make code folding easier; if I wrap a section of code in an #if 1 ... #endif, I can fold it in my editor. (The code in question is very macro-heavy, and not written by me, so more traditional ways of making a huge block of code manageable won't work.)

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

    One of the fundamental properties of software is that computer program is cheap to modify.

    That's why certain code is written in such a way that it will make modification easier. That's why they need various patterns, like "interface", or "proxy".

    And that's why you sometimes see weird constructs like #if 1-#else-#endif, an only purpose of which is to easily switch the part of code that will be compiled, by small effort: changing 1 to 0.

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