#if 1 statements #else other statements

In the following preprocessor code what is the use of statements and other statements?
When will the other statements be used?

Please explain what is the use or purpose of the following preprocessor code?

Thanks

1
2
3
4
5
#if 1
statements
#else
other statements
#endif 



What is the real use of above code please?
Frequently I use following to comment a bunch of program code.


1
2
3
4
#if 0
statements
#endif



Last edited on
preprocessor works like C++ in this sense.
if 1 means if(true) means the if statement executes.

that means the else won't execute.

usually its
#if symbol
statements
#else
statements
#endif //required because {} does not work in preprocessor.

what this is used for, most of the time, is to help set up different build options when you compile the code, for example you may have if windows include windows.h else include unixstd.h or something like that, to compile on windows or unix, respectively. Here, instead of like in code, the statements in the blocks are compiled or not compiled depending on the condition.

if 1 and if 0 are usually temporary to debug by removing a block of code, same as /**/ wrapper around it, but useful if the code in question HAS comment blocks inside it already. You don't normally KEEP the 0/1 macros after debugging the code, they are a temporary tool for use during development and debugging etc.

you may also see #ifdef -- this checks the *existence* of the symbol instead of its value.
Last edited on
A few things,

/* */ style comments cannot be nested, so it's harder to comment out huge swaths of code if there are already /**/ comments in them (of course, with IDEs these days, this is less than an issue, but sometimes you might just want to edit something quickly in a text editor).

Using #if 0 as a comment allows for nesting, so you can have an #if 0 within another #if 0.

It allows you to just toggle something easily that a dev is working on. Change the 1 to a 0 to trigger the "other statements". This is more of an ad hoc thing when you're using literal 1s or 0s.

It becomes more useful when you inject macros from the command-line or from other files.
So you can do something like:
1
2
3
4
5
#ifdef USE_CONFIG_A
    do_something();
#else
    do_something_else();
#endif 

And the user can pass in a macro like -DUSE_CONFIG_A from the command-line or generated from some make system.
Last edited on
What is the real use of above code please?

Oversimplified example. The condition 1 is static and constant -- useless.

https://docs.microsoft.com/en-us/cpp/preprocessor/hash-if-hash-elif-hash-else-and-hash-endif-directives-c-cpp?view=msvc-170 has more realistic examples,
where condition depends on something that you can change without modifying the code.
They're used to comment out blocks of code quickly and safely.
A good case study could be the definition of assert from the cassert header:

1
2
3
4
5
#if defined NDEBUG
#  define assert(expr)
#else
#  define assert(expr) (_Some_implementation_specific_function((bool)(expr), _STRINGIFY(expr)))
#endif 
Last edited on
Thank you all for the excellent information.
Best regards,
Registered users can post here. Sign in or register to post.