注释用于向读取代码的人指示某些内容。注释被编译器视为空白,不会改变代码的实际含义。C
语言中有两种注释语法,原始的/* */
和稍微新一点的//
。一些文档系统如Doxygen
使用特殊格式的注释来帮助生成代码的文档。
使用预处理进行注释
还可以使用预处理器指令 # if 0
和 # endf
“注释掉”大块代码。当代码包含多行注释(否则不会嵌套)时,这非常有用
如下
#if 0 /* Starts the "comment", anything from here on is removed by preprocessor */
/* A large amount of code with multi-line comments */
int foo()
{
/* lots of code */
...
/* ... some comment describing the if statement ... */
if (someTest) {
/* some more comments */
return 1;
}
return 0;
}
#endif /* 0 */
/**/
只认第一个*/
/* this comment is on its own line */
if (x && y) { /*this comment is at the end of a line */
if ((complexCondition1) /* this comment is within a line of code */
&& (complexCondition2)) {
/* this comment is within an if, on its own line */
}
}
'//'请注意??\
int main() {
// Why did I do this ??/
unsigned char a = 5; // 00000101
unsigned char b = 9; // 00001001
printf("a & b = %d\n", a & b); // 00000001
printf("a | b = %d\n", a | b); // 00001101
printf("a ^ b = %d\n", a ^ b); // 00001100
printf("~a = %d\n", a = ~a); // 11111010
printf("b << 1 = %d\n", b << 1); // 00010010
printf("b >> 1 = %d\n", b >> 1); // 00000100
int x = 20;
return 0;
}
这会导致编译错误
<source>: In function 'int main()':
<source>:22:28: error: 'a' was not declared in this scope
22 | printf("a & b = %d\n", a & b); // 00000001
| ^
??/
实际上是\
的手写表示法,\
是行延续符号。这意味着编译器认为下一行是当前行的延续,即注释的延续。