PDA

View Full Version : Why use #if 0 in code?


nv_dv
28-Dec-2006, 06:44
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:

Davros
28-Dec-2006, 06:51
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

Simon F
28-Dec-2006, 07:56
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.

/*
.....
*/
...is a multi-line comment in C.


The advantage of using the pre-processor

#if 0
.........
#endif
is that it can be "stacked". The /*..*/ can't.

Nick
28-Dec-2006, 08:00
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.

Tokelil
28-Dec-2006, 10:54
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... :evil:

Rys
28-Dec-2006, 11:39
Anyway, the headline isn't really descriptive of the thread content though... :evil:
Fixed. OP, try and title your threads appropriately in the future, if possible :smile:

Simon F
28-Dec-2006, 11:56
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.

Xmas
28-Dec-2006, 11:57
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).

Graham
28-Dec-2006, 12:40
Just to explain what Simon.F said: (they can stack...)


/*

... (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


#if 0

... (1)

/*omg a real comment*/

... (2)

#endif


would be ok

nv_dv
29-Dec-2006, 07:33
now i get it


-headline_str- has been resolved :eek:
i'll be more descriptiv
thanx for the "move"