Lexical Elements of C - Comments

Comments begin with the /* characters, end with the */ characters, and can span more than one line. You can put comments anywhere the language allows white space. Multibyte characters can be included in a comment.

Comments are replaced during preprocessing by a single space character.

If the -qcpluscmt compiler option is in effect when you compile a C program, double slashes (//) also specify the beginning of a comment. The comment ends at the next newline character.

The -C compiler option affects how comments appear in the compiler listing.

The -Pc compiler option affects how comments appear in the compiler listing.

Note: The /* or */ characters found in a character constant or string literal do not start or end comments.

You cannot nest comments. Each comment ends at the first occurrence of */. For example, in the following code segment, the comments are highlighted:

 1   /* A program with nested comments. */
 2
 3   #include <stdio.h>
 4
 5   int main(void)
 6   {
 7      test_function();
 8   }
 9
10   int test_function(void)
11   {
12      int number;
13      char letter;
14   /*
15   number = 55;
16   letter = 'A';
17   /* number = 44; */
18   */
19   return 999;
20   }

In test_function, the compiler reads the /* in line 14 through the */ in line 17 as a comment, and line 18 as C language code, causing errors at line 18.

To avoid commenting over comments already in the source code, you can use conditional compilation preprocessor directives to cause the compiler to bypass sections of a C program. For example, one method to ignore lines 15 through 17 would be to change line 14 to:

14   #if 0

and line 18 to:

18   #endif

To later reenable the ignored comments, change line 14 to:

14   #if 1


Lexical Elements - Tokens
Lexical Elements - Identifiers
Lexical Elements - Constants
Preprocessor Directives
Conditional Compilation Directives


#if Preprocessor Directive
#endif Preprocessor Directive