Cprogramming 简明教程

Comments in C

在 C 程序中使用 comments 会增加代码的可读性。您必须在适当的位置用注释穿插代码。就编译器而言,注释会被忽略。在 C 中,注释是一行或多行文本,编译器在构建机器代码时会跳过这些文本。

Using comments in a C program increases the readability of the code. You must intersperse the code with comments at appropriate places. As far as the compiler is concerned, the comments are ignored. In C, the comments are one or more lines of text, that the compiler skips while building the machine code.

当程序需要修改时,C 中的 comments 发挥了重要作用,特别是对于非原始编写者而言。程序员经常不会重视添加注释,但有效地使用注释对于提高代码质量非常重要。

The comments in C play an important part when the program needs to be modified, especially by somebody else other than those who have written it originally. Putting comments is often not given importance by the programmer, but using them effectively is important to improve the quality of the code.

Why to Use Comments in C Programming?

与英语等任何人类语言相比,包括 C 语言在内的任何编程语言都是不冗长的。它的 keywords 数量要少得多,C 是仅包含 32 个关键字的最小语言之一。因此,C 程序中的指令可能难以理解,特别是对于没有编程背景的人。

Any programming language, including C, is less verbose as compared to any human language like English. It has far less number of keywords, C being one of the smallest languages with only 32 keywords. Hence, the instructions in a C program can be difficult to understand, especially for someone with non-programming background.

C language syntax 也有所不同,也很复杂。通常,程序员会尝试添加复杂性以优化代码。然而,它使得代码难以理解。 Comments provide a useful explanation and convey the intention behind the use of a particular approach

The C language syntax also varies and is also complicated. Usually, the programmer tries to add complexity to optimize the code. However, it makes the code hard to understand. Comments provide a useful explanation and convey the intention behind the use of a particular approach.

例如,以 C 中的 ?: 运算符为例,它是 if-else statement 的快捷方式。

For example, take the case of the ?: operator in C, which is a shortcut for the if-else statement.

因此,可以使用以下语句,以替代以下代码 -

So, instead of the following code −

if (a % 2 == 0){
   printf("%d is Even\n", a);
} else {
   printf("%d is Odd\n", a);
}

可以运用以下语句 -

One can use the following statement −

(a % 2 == 0) ? printf("%d is Even\n", a) : printf("%d is Odd\n", a);

显然,第二个方法比第一个方法更复杂。如果添加有用的注释,就会更容易理解所用语句的意图和逻辑。

Obviously, the second method is more complicated than the first. If useful comments are added, it makes easier to understand the intention and logic of the statement used.

Types of Comments in C

在 C 语言中,注释有两种类型 -

In C, there are two types of comments −

  1. Single-line comments

  2. Multi-line comments

Single-line Comment in C

C++ 样式的单行注释已与 C99 标准一起并入 C 编译器中。如果任何文本以 // 符号开头,则其余行将被视为注释。

The C++-style single-line comments were incorporated in C compilers with C99 standards. If any text begins with the // symbol, the rest of the line is treated as a comment.

在代码中,由双斜线或正斜杠 [ // ] 后跟的文本被视为单行注释。 // 之后的文本在编译过程中 C compiler 会全部忽略。与多行或块注释不同,它不必关闭。

The text followed by a double oblique or forward slash [//] in a code is treated as a single-line comment. All that text after // is ignored by the C compiler during compilation. Unlike the multi-line or block comment, it need not be closed.

//comment text

// 符号可以出现在任何地方。它表示,它之后的直到行结束的所有文本都是注释。编辑器中的后续行又是一个编写有效的 C 语句的地方。

The // symbol can appear anywhere. It indicates that all the text following it till the end of the line is a comment. The subsequent line in the editor is again a place to write a valid C statement.

查看以下程序,了解我们在其 main 函数中如何使用单行注释 -

Take a look at the following program and observe how we have used single-line comments inside its main function −

/* Online C Compiler and Editor */
#include <stdio.h>
#include <math.h>

/*forward declaration of function*/
float area_of_square(float);

float area_of_square(float side){
   float area = pow(side,2);
   return area;
}

// main function - entire line is a comment
int main(){

   // variable declaration (this comment is after the C statement)
   float side = 5.50;

   float area = area_of_square(side);   // calling a function
   printf ("Side = %5.2f Area = %5.2f", side, area);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Side = 5.50 Area = 30.25

Multi-line Comment in C

early versions of C (ANSI C) 中,置于符号 // 之间的任何长度的文本都将被视为注释。该文本可能会跨代码文件中的多行。您可以称之为多行注释。将连续行的块视为注释。

In early versions of C (ANSI C), any length of text put in between the symbols / and / is treated as a comment. The text may be spread across multiple lines in the code file. You may call it a multi-line comment. A block of consecutive lines is treated as a comment.

多行注释的一般结构如下 -

The general structure of a multi-line comment is as follows −

/* The comment starts here
Line 1
Line 2
…..
…..
Comment ends here*/

例如 -

For example −

/*
Example of a multi-line comment
program to print Hello World
using printf() function
*/

显然,由于注释被编译器忽略,C 语言的语法规则不适用于注释文本。

Obviously, since the comments are ignored by the compiler, the syntax rules of C language don’t apply to the comment text.

Comments 可以出现在程序中的任何位置,在顶部、在代码之间,或者在 functionstruct declaration 的开头等。

Comments can appear anywhere in a program, at the top, in between the code, or at the beginning of a function or a struct declaration, etc.

在此示例中,我们有一个多行注释,它解释了在给定代码中使用的特定用户定义函数的作用 -

In this example, we have a multi-line comment that explains the role of a particular user-defined function used in the given code −

/* program to calculate area of square */

/* headers */
#include <stdio.h>
#include <math.h>

/* forward declaration of function */
float area_of_square(float);


/* main function */
int main(){

   /* variable declaration */
   float side = 5.50;

   /* calling function */
   float area = area_of_square(side);
   printf("Side = %5.2f Area = %5.2f", side, area);

   return 0;
}

/* User-defined function to calculate
the area of square. It takes side as the argument
and returns the area */

float area_of_square(float side){
   float area = pow(side, 2);
   return area;
}

执行代码后,它将产生以下输出 -

When you execute the code, it will produce the following output −

Side = 5.50 Area = 30.25

在插入注释时,您必须确保对于以 / 开头的每个注释,都必须有对应的 / 符号。如果您以 / * 开始注释却没有关闭它,那么编译器将抛出一个错误。

While inserting a comment, you must make sure that for every comment starting with /, there must be a corresponding / symbol. If you start a comment with /* and fail to close it, then the compiler will throw an error.

Note: 阻塞注释或多行注释必须置于 // 符号内,而单行注释以 // 符号开头并一直有效到行尾。

Note: A blocked comment or multi-line comment must be put inside / and / symbols, whereas a single-line comment starts with the // symbol and is effective till the end of the line.

鼓励在程序中插入注释。程序员通常避免添加注释的做法。然而,如果不正确注释代码,有时他们会发现调试和修改代码困难。注释在协同开发时尤其至关重要。有效地使用注释将对团队的所有成员有帮助。

Placing comments in a program is always encouraged. Programmers usually avoid the practice of adding comments. However, they can sometimes find it difficult to debug and modify their code if it is not properly commented. Comments are especially vital when the development is done in collaboration. Using comments effectively can be of help to all the members of a team.

尽管编译器会忽略注释,但注释应在含义上清晰简洁。每当代码需要修改时,都应在注释中提及原因、时间戳和作者。

Even though comments are ignored by the compiler, they should be clear in meaning and concise. Whenever the code needs modification, the reason, the timestamp, and the author should be mentioned in comments.