Commenting out code with the preprocessor

This tip submitted by Webmaster on 2005-01-24 14:16:39. It has been viewed 38865 times.
Rating of 5.7 with 193 votes



Because C and C++ do not allow nesting comments with /* and */, it can be a hassle to comment out long regions of code.

For instance, you cannot do this:

/* First layer
    /* Second layer */
*/


because the first */ closes the entire comment!


Fortunately, the preprocessor can help you: you can use the #if .. #endif combination to cause the preprocessor to avoid compiling any portion of your code by using a condition that will never be true.

#if 0
/* comment */
/* code */
/* comment */
#endif






More tips

Help your fellow programmers! Add a tip!