Thread: What does "#if 1" mean?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    What does "#if 1" mean?

    I ran across the following statements in some C++ code.

    Code:
    #if 1
    	statements();
    #else
    	more_statements();
    #endif
    What does "#if 1" mean? More specifically, I'm lost on what the "1" part means.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Okay. So
    Code:
    #if 1
    is the same as
    Code:
    if ( a_horse == a_horse)

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by thetinman
    Okay. So
    Code:
    #if 1
    is the same as
    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 executable
    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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #if 1
    	statements();
    #else
    	more_statements();
    #endif
    The compiler will never see more_statements();
    The pre-processor will remove it before passing the result to the compiler.

    Code:
    if ( 1 ) {
    	statements();
    } else {
    	more_statements();
    }
    The compiler will see more_statements(), and may include it in your final code.
    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.

  7. #7
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    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:
    Code:
    ...
    cout << "var 1: " << var1 << "\n";
    cout << "var 2: " << var2 << "\n";
    cout << "var 3: " << var3 << "\n";
    cout << "var 4: " << var4 << "\n";
    ...
    Now imagine you want to compile without this temporary code, you'll need to:
    • 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)
    Or, you can use the trick:
    Code:
    ...
    #if 1
    cout << "var 1: " << var1 << "\n";
    cout << "var 2: " << var2 << "\n";
    cout << "var 3: " << var3 << "\n";
    cout << "var 4: " << var4 << "\n";
    #endif
    ...
    Like this you can turn off the whole code block simply by changing the #if line to:
    Code:
    #if 0
    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 1
    The preprocessor will see it as true (1) and will compile it again.

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does a the "%" sign mean? as in "if (i % x==0)"?
    By thetinman in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:11 AM
  2. [newb] How is "!(1 && !(0 || 1))" true?
    By eddwills in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 08:19 AM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM