非常教程

C参考手册

C 语法

Comments

评论可以作为一种内部代码文档。当插入到程序中时,它们被编译器有效地忽略; 它们仅仅是用来作为阅读源代码的人的笔记。

句法

/* comment */

(1)

// comment\n

(2)

(since C99)

1)通常被称为“C型”或“多线”评论。

2)通常被称为“C ++风格”或“单行”的评论。

在翻译阶段3通过用一个空格字符替换每个评论来从程序中删除所有评论。

C-风格

C 风格的评论通常用于评论大块文本或小部分代码; 但是,它们可以用来评论单行。要将文本插入为C风格的评论,只需使用/*和环绕文本即可*/。C风格的注释告诉编译器忽略/*和之间的所有内容*/。虽然它不是 C 标准的一部分,/**并且*/通常用于指示文档块; 这是合法的,因为第二个星号被简单地视为评论的一部分。

除了字符常量,字符串文字或注释之外,这些字符会/*引入注释。这种评论的内容仅用于识别多字节字符并查找*/终止评论的字符。C风格的评论不能嵌套。

C ++风格的C ++风格的评论通常用于评论单行文本或代码; 但是,它们可以放在一起形成多行评论。要将文本作为C ++样式的注释插入,只需在文本前面加上//,然后按照新的行字符跟随文本。C ++风格的注释告诉编译器忽略//和新行之间的所有内容。除了字符常量,字符串文字或注释之外,字符//会引入一个注释,其中包含所有多字节字符,但不包括下一个换行符。这种注释的内容仅用于识别多字节字符并查找终止注释的换行符。C ++风格的注释可以嵌套:// y = f(x); //调用算法C风格的注释可能出现在C ++风格的注释中:// y = f(x); / *调用算法* / C风格的注释可能出现在C风格的注释中; 这是排除一小块源代码的机制:/ * y = f(x); //调用算法z = g(x); * /

(自C99以来)

注意

因为在预处理器阶段之前删除了注释,所以不能使用宏来形成注释,并且未终结的 C 样式注释不会从#include 的文件溢出。

/* An attempt to use a macro to form a comment. */
/* But, a space replaces characters "//".       */
#ifndef DEBUG
    #define PRINTF //
#else
    #define PRINTF printf
#endif
...  
PRINTF("Error in file %s at line %i\n", __FILE__, __LINE__);

除了注释外,用于源代码排除的其他机制还有:

#if 0
    puts("this will not be compiled");
    /* no conflict with C-style comments */
    // no conflict with C++-style comments
#endif
if(0) {
    puts("this will be compiled but not be executed");
    /* no conflict with C-style comments */
    // no conflict with C++-style comments
}

在 C99中引入//注释在一些罕见的情况下是一个突破性的改变:

a = b //*divisor:*/ c
+ d; /* C89 compiles a = b / c + d;
        C99 compiles a = b + d; */

/*
C-style comments can contain
multiple lines.
*/
 
/* Or, just one line. */
 
// C++-style comments can comment one line.
 
// Or, they can
// be strung together.
 
int main(void)
{
  // The below code won't be run
  // return 1;
 
  // The below code will be run
  return 0;
}

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.4.9 Comments (p: 75)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.4.9 Comments (p: 66)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.1.9 Comments

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comparison operators
30.compound literals
31.Conditional inclusion
32.Conformance
33.const type qualifier
34.Constant expressions
35.continue statement
36.Declarations
37.do-while loop
38.Enumerations
39.Escape sequences
40.Expressions
41.External and tentative definitions
42.File scope
43.floating constant
44.for loop
45.Function declarations
46.Function definitions
47.Functions
48.Generic selection
49.goto statement
50.Identifier
51.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。