C 注释C Comments

“注释”是一个以正斜杠/星号组合(/*)开头的字符序列,编译器会将正斜杠/星号组合视为单个空白字符,要不然就忽略它。A "comment" is a sequence of characters beginning with a forward slash/asterisk combination (/*) that is treated as a single white-space character by the compiler and is otherwise ignored. 注释可以包含可表示字符集中的任意字符组合,包括换行符,但“结束注释”分隔符 (*/) 除外。A comment can include any combination of characters from the representable character set, including newline characters, but excluding the "end comment" delimiter (*/). 注释可以占用多行,但无法嵌套。Comments can occupy more than one line but cannot be nested.

注释可以出现在允许使用空白字符的任何地方。Comments can appear anywhere a white-space character is allowed. 由于编译器将注释视为空白字符,因此不能将注释包含在标记中。Since the compiler treats a comment as a single white-space character, you cannot include comments within tokens. 编译器将忽略注释中的字符。The compiler ignores the characters in the comment.

使用注释来记录您的代码。Use comments to document your code. 本示例是编译器接受的注释:This example is a comment accepted by the compiler:

/* Comments can contain keywords such as  
   for and while without generating errors. */  

注释可以与代码语句出现在同一行中:Comments can appear on the same line as a code statement:

printf( "Hello\n" );  /* Comments can go here */  

可以选择在函数或程序模块前面放置一个描述性注释块:You can choose to precede functions or program modules with a descriptive comment block:

/* MATHERR.C illustrates writing an error routine   
 * for math functions.   
 */   

由于注释不能包含嵌套注释,因此本示例导致了一个错误:Since comments cannot contain nested comments, this example causes an error:

/* Comment out this routine for testing   

   /* Open file */  
    fh = _open( "myfile.c", _O_RDONLY );  
    .  
    .  
    .  
 */  

该错误发生的原因是编译器将单词 */ 后的第一个 Open file 识别为注释的末尾。The error occurs because the compiler recognizes the first */, after the words Open file, as the end of the comment. 编译器尝试处理剩余的文本,当它在注释外找到 */ 时,便产生了错误。It tries to process the remaining text and produces an error when it finds the */ outside a comment.

尽管您可以出于测试目的使用注释来呈现某些处于非活动状态的代码行,但预处理器指令 #if#endif 以及条件编译都是执行此任务的有用的替代选择。While you can use comments to render certain lines of code inactive for test purposes, the preprocessor directives #if and #endif and conditional compilation are a useful alternative for this task. 有关详细信息,请参阅预处理器参考中的预处理器指令For more information, see Preprocessor Directives in the Preprocessor Reference.

Microsoft 专用Microsoft Specific

Microsoft 编译器还支持前面有两个正斜杠 (//) 的单行注释。The Microsoft compiler also supports single-line comments preceded by two forward slashes (//). 如果使用 /Za(ANSI 标准)进行编译,这些注释将产生错误。If you compile with /Za (ANSI standard), these comments generate errors. 这些注释不能扩展到第二行。These comments cannot extend to a second line.

// This is a valid comment  

以两个正斜杠 (//) 开头的注释被前面没有转义字符的下一个换行符终止。Comments beginning with two forward slashes (//) are terminated by the next newline character that is not preceded by an escape character. 在下一个示例中,换行符的前面有一个反斜杠 (\),这将创建“转义序列”。In the next example, the newline character is preceded by a backslash (\), creating an "escape sequence." 此转义序列会使编译器将下一行视为上一行的一部分。This escape sequence causes the compiler to treat the next line as part of the previous line. (有关详细信息,请参阅转义序列。)(For more information, see Escape Sequences.)

// my comment \  
    i++;   

因此,i++; 语句被注释掉。Therefore, the i++; statement is commented out.

Microsoft C 的默认设置是启用 Microsoft 扩展。The default for Microsoft C is that the Microsoft extensions are enabled. 请使用 /Za 禁用这些扩展。Use /Za to disable these extensions.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

C 标记C Tokens