Microsoft KB Archive/39312
HOWTO: Comment Out Blocks of Code and Comments for Debugging
Article ID: Q39312
The information in this article applies to:
The C language does not support the use of nested comments. This might appear to be a limitation in cases where it is necessary, perhaps for debugging purposes, to comment out a block of code that contains comments. However, this situation can be overcome by using the much more powerful preprocessor commands #if and #endif.
The #if preprocessor command evaluates a constant expression to either true (which has the value 1) or false (which has the value 0) to perform conditional compilation. If the expression equates to true, the code segment will be compiled. If it equates to false, it will be ignored by the compiler. Therefore, if the syntax below is used, the enclosed block of code will be forever ignored by the compiler, giving a convenient method of effectively commenting out a block of code.
Because the expression in the #if can be any constant expression, you can make complicated tests. You can even use the /Dconst=value switch on the CL command line to set a preprocessor symbol that will include or exclude the debugging code when you compile.
The following is a simple example:
#define OUT 0 #if OUT /* will always equate to "false" */ /* code and comments that you wish to remove are here */ #endif
In addition to this method, if you are using one of the Microsoft C++ development tools listed above, you can use a simpler technique in new code where ANSI compliance is not required. This technique relies on the new single line comment allowed by C++, which Microsoft compilers also allow in C code. Such code, however is not ANSI standard and will generate a C2143 syntax error on any lines using the // single line comment. To allow easy commenting of blocks of code, the rule is to ALWAYS use single line comments for your source code comments. Since single line comments can be nested inside standard comments. For example:
i = x; // always compile /* printf ("i = %d\n",i); // debug only */
This technique, of course, does not allow compile-time decisions about what to include as the #if technique does. However, it is very convenient when making changes to the code or trying to find a bug by removing non- essential statements.
For more information on this topic, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q102390 TITLE : INFO: Keeping #include Files Out of VWB Dependency List
Keywords : kbLangC kbVC Version : MS-DOS:6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5,1.51,1.52; WINDOWS NT:1.0,2.0,2.1,2.2,4.0,5.0 Platform : MS-DOS NT WINDOWS Issue type : kbhowto
Last Reviewed: August 28, 1997