Why use #if 0 in code?

nv_dv

Newcomer
Hi

isn't 3d but most c programming question


i saw this thing often
-----------
#if 0
-----------

then i don't get from what it is checked


any idea about the meaning of this ?

hum.... open a thread just for that :runaway:
 
its a way of commenting out a large section of code with out putting /* (or whatever the comment symbol is for c) on every line
 
Last edited by a moderator:
its a way of commenting out a large section of code with out putting /* (or whatever the comment symbol is for c) on every line
You don't need one on every line in C.
Code:
/*   
.....
*/
...is a multi-line comment in C.


The advantage of using the pre-processor
Code:
#if  0
.........
#endif
is that it can be "stacked". The /*..*/ can't.
 
Another advantage is that you can create an if-else block, so you can select between two implementations by just changing 0 to 1 or reverse.
 
I have been wondering the same myself, so thanks for answering. The "problem" is that the highlighter wont color it as comment though...

Anyway, the headline isn't really descriptive of the thread content though... :devilish:
 
I have been wondering the same myself, so thanks for answering. The "problem" is that the highlighter wont color it as comment though...
That rather depends on the syntax parser in the editor. Some actually do shade "#if 0..." regions differently.
 
I have been wondering the same myself, so thanks for answering. The "problem" is that the highlighter wont color it as comment though...
VC2005 shows it as inactive block (grey), but that's almost as good as a comment (green).
 
Just to explain what Simon.F said: (they can stack...)

Code:
/*

... (1)

/*omg a real comment*/

... (2)

*/

won't always work, the first */ ends the comment block.. So the compiler thinks (2) is uncommented, hits the */ and breaks.

where

Code:
#if 0

... (1)

/*omg a real comment*/

... (2)

#endif

would be ok
 
Last edited by a moderator:
Back
Top