I ran across the following statements in some C++ code.
What does "#if 1" mean? More specifically, I'm lost on what the "1" part means.Code:#if 1 statements(); #else more_statements(); #endif
This is a discussion on What does "#if 1" mean? within the C++ Programming forums, part of the General Programming Boards category; I ran across the following statements in some C++ code. Code: #if 1 statements(); #else more_statements(); #endif What does "#if ...
I ran across the following statements in some C++ code.
What does "#if 1" mean? More specifically, I'm lost on what the "1" part means.Code:#if 1 statements(); #else more_statements(); #endif
1 is true (and 0 is false). The code works similarly to your basic if/else statement but with a difference. The difference is, since these are preprocessor commands, the resulting program will be slightly different depending on whether that 1 is present or a 0 is present. In a traditional if/else bit of code, the if part and the else part are compiled into the final program, then the correct yes/no branch is determined at run-time based on some value. In your example however, as long as that 1 is present, the resulting compiled program will contain a call to the statements function and not the more_statements function at that point in the code. When you change the 1 to a 0 and then recompile/relink the program, the more_statements function will be called at that stage of the program instead of the statements function. Thus, this now becomes a compile time decision made as to which code to call at a particular stage of the final executable.
I used to be an adventurer like you... then I took an arrow to the knee.
In C++, 0 evaluates to false in a boolean context(e.g. an if statement), and any non-zero number, e.g. -10 or 3, evaluates to true.
Okay. So
is the same asCode:#if 1
Code:if ( a_horse == a_horse)
No, not really. #if 1 statements after the #else do not even get compiled, while the statement you posted are executableOriginally Posted by thetinman
Code:#if 1 // The compiler only procecesses these satements #else // the preprocessor tosses out these statements // so the compiler does not even see them. #endif
The compiler will never see more_statements();Code:#if 1 statements(); #else more_statements(); #endif
The pre-processor will remove it before passing the result to the compiler.
The compiler will see more_statements(), and may include it in your final code.Code:if ( 1 ) { statements(); } else { more_statements(); }
On the other hand, it may decide that the code can never be reached and remove it for you.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
From code i've seen that is often used as a cheat to allow easily turning on/off debug or temporary code. For instance, imagine you had a 5-6 lines of temporary code in the middle of other code:
Now imagine you want to compile without this temporary code, you'll need to:Code:... cout << "var 1: " << var1 << "\n"; cout << "var 2: " << var2 << "\n"; cout << "var 3: " << var3 << "\n"; cout << "var 4: " << var4 << "\n"; ...
Or, you can use the trick:
- delete it (if it's debug code you might need it later so you'll need to rewrite it)
- comment it (by adding // to each line or by adding /* and */ before and after that code section respectively)
Like this you can turn off the whole code block simply by changing the #if line to:Code:... #if 1 cout << "var 1: " << var1 << "\n"; cout << "var 2: " << var2 << "\n"; cout << "var 3: " << var3 << "\n"; cout << "var 4: " << var4 << "\n"; #endif ...Since the preprocessor will see it as false (0) the following block of code won't be compiled. If you change it back to:Code:#if 0The preprocessor will see it as true (1) and will compile it again.Code:#if 1
IMO this is not the best way to do this, i think you'd be better using SYMBOLS for achieving this purpose, but this is quite a common use for #if 1.
Cheers