#if #endif directives

Feb 26, 2009 at 3:11pm
1)
Does an #if directive always preceeds a constant expression with a condition? If so, based on the condition, it executes the code upto #endif.

Eg:
int a = -1;
#if a<0
cout << "Negative a" << endl;
#endif
cout << "Non-negative a" << endl;

Why would I use #if #endif directives instead of if-else loop?

2)
Could #if directive preceed a constant number? What would the implicit condition be in that case?

Eg:
#if 0

What is the implicit condition in the case above?

Thanks.
Last edited on Feb 26, 2009 at 3:16pm
Feb 26, 2009 at 3:37pm
The preprocessor runs before the compiler. It's possible to see the preprocessor output if you want to be clear on what happens.

In example 1, there are two different a. #if a<0 expects a to be defined using #define or on the command line.

int a = -1; is not touched by the preprocessor at all. That a and the a that the preprocessor sees are different.

Expressions evaluate to zero or non-zero and can control #if. In example 2, #if 0 always evaluates to false and code in that block is excluded.
Feb 26, 2009 at 3:58pm
#if and similar directives are used for conditional compilation and header guards

http://www.cplusplus.com/doc/tutorial/preprocessor.html
Feb 26, 2009 at 4:56pm
Thanks for replies.

>>In example 2, #if 0 always evaluates to false

Does it mean that the code with that #if-#endif won't be executed? Then why is the expression needed in the first place?

Which condition is being evaluated to false?
Feb 26, 2009 at 5:00pm
Correct, the code in #if 0 will not be compiled.

This is often used to comment out blocks of code.
Feb 26, 2009 at 5:21pm
So basically, I'd put it just to remind me of something when I see that code segment, and that's it. Is that correct?

That seems to be a replacement for commenting out code. Ofcourse comments get compiled, as opposed to these, that don't even get compiled.
Feb 26, 2009 at 5:40pm
Comments don't get compiled.
#if are used with #defined constants, having #if 0 doesn't make much sense.

This could be a way of using that:
1
2
3
4
5
6
7
8
//#ifdef == #if defined
#ifdef __cplusplus
    //Using a C++ compiler
   cout << "Hello";
#else
   //Using a C compiler
   printf("Hello");
#endif 

__cplusplus is defined by the compiler
Last edited on Feb 26, 2009 at 5:42pm
Feb 26, 2009 at 5:57pm
having #if 0 doesn't make much sense.


It makes lots of sense because it can comment out blocks of code which contain sectional comments:

1
2
3
4
5
6
7
8
9
#if 0

void blah()
{
 /* There is no easier way to comment out this section of code */
 DoStuff();
}

#endif 
Feb 26, 2009 at 5:58pm
Thanks for the answers!
Feb 26, 2009 at 6:45pm
#if 0 makes no sense whatsoever. Unless, I guess, if you have C style comments all through your code...ok, so in C, it makes sense. I spoke too soon.

Now, in C++, I could see something like this where some identifier is used (and DEBUG may be 0):
1
2
3
#if DEBUG
//...
#endif 
Last edited on Feb 26, 2009 at 6:47pm
Feb 26, 2009 at 6:47pm
you don't ever use "C-style" comments in your code?

I find myself sometimes having paragraphs with function details preceeding a function body. Block comments are a godsend for something like that.
Last edited on Feb 26, 2009 at 6:50pm
Feb 26, 2009 at 7:51pm
I'm sorry, disregard my mindless blabble above... It would be handy for commenting out code in development. Somehow I got my thoughts mixed together...if I saw an #if 0 checked into version control on a project, I would be going "wtf."
Topic archived. No new replies allowed.