如何在C编程中编写注释

C 语言中的注释是什么?

注释是对程序源代码的解释或描述。它可以帮助开发人员解释代码的逻辑,并提高程序的可读性。在运行时,注释会被编译器忽略。

C 语言中有两种类型的注释

1) 以斜杠星号 /* 开头,以星号斜杠 */ 结尾的注释,可以放在代码中的任何位置,可以是同一行或多行。

2) 单行注释,使用双斜杠 // 来注释单行

单行注释示例

// single line comment example

这是注释类型的示例

// C program to demo 
// Single Line comment 
#include <stdio.h> 
int main(void) 
{ 
  
	// This is a single line comment 
	printf("Guru99"); 
	return 0;  // return zero
}

多行注释示例

/* Sample Multiline Comment
Line 1
Line 2
….
…
*/

多行注释示例

#include <stdio.h>
int main() {
/* in main function
I can write my principal code
And this in several comments line */
int x = 42; //x is a integer variable
printf("%d", x);
return 0;}

为什么需要注释?

一个能写出人类可读代码的优秀程序员,比一个只能写出机器可读代码的程序员更好。

因此,强烈建议在代码中插入注释,因为这是良好的编程实践。注释不会影响程序,因为 编译器 会忽略它们。

如果开发人员在很长时间后重新查看代码,注释有助于他们理解代码的逻辑/算法。