Error, in C language, the comment part has no impact on the running results of the program. It can appear anywhere in the program. There are two types of comments in C language: one is a block comment starting with "/*" and ending with "*/"; the other is a single-line comment starting with "//" and ending with a newline character.
The operating environment of this article: windows10 system, c11, thinkpad t480 computer.
Related recommendations:C language video tutorial
Comments in C language
When writing C language source When coding, you should use more comments to help understand the code. There are two comment methods in C language:
One is a block comment starting with /* and ending with */;
The other is a single-line comment starting with // and ending with a newline character.
You can use /* and */ delimiters to mark comments within one line, or to mark comments on multiple lines. For example, in the following function prototype, the ellipsis means that the open() function has a third parameter, which is optional. The comment explains the usage of this optional parameter:
int open( const char *name, int mode, … /* int permissions */ );
You can use // to insert a whole line of comments, or write the source code in a two-column format, with the program in the left column and the comments in the right column:
const double pi = 3.1415926536; // pi是—个常量
In the C99 standard, single-line comments were officially added to the C language, but most compilers had begun to support this usage before C99. Sometimes they are called "C-style" comments, but in fact they are derived from BCPL, the predecessor of C.
Position of comments
In C language, the comment part has no effect on the running results of the program. It can appear anywhere in the program.
Example:
int/*....*/i; //正确 char* s="abcdefgh //hijklmn"; //正确 in/*...*/ti; //错误注释会被空格替换 //注意: /*...*/不能嵌套 ,/*总是与离他最近的*/匹配 y=x/*p // 该语句由于没有找到*/ 会报错 //要实现以上功能 可以用y=x/(*p)或y=x/ *p代替
Comment specifications
2-1: Under normal circumstances, the amount of effective comments in the source program must be more than 20%.
Note: The principle of comments is to help the reading and understanding of the program. They should be added where needed. Comments should not be too many or too few. The comment language must be accurate, easy to understand, and concise.
2-2: The file header should be commented, and the comments must list: copyright statement, version number, generation date, author, content, function, modification log, etc.
Example: The header comments of the following header file are relatively standard. Of course, they are not limited to this format, but the above information is recommended to be included.
/***************************************************************************** Copyright: 1988-1999, Huawei Tech. Co., Ltd. File name: 文件名 Description: 用于详细说明此程序文件完成的主要功能,与其他模块或函数的接口,输出值、取值范围、含义及参数间的控制、顺序、独立或依赖等关系 Author: 作者 Version: 版本 Date: 完成日期 History: 修改历史记录列表, 每条修改记录应包括修改日期、修改者及修改内容简述。 *****************************************************************************/
2-3: The function header should be commented, listing: the purpose/function of the function, input parameters, output parameters, return value, calling relationship (function, table), etc.
Example: The following function comments are relatively standard. Of course, they are not limited to this format, but the above information is recommended to be included.
/************************************************* Function: // 函数名称 Description: // 函数功能、性能等的描述 Calls: // 被本函数调用的函数清单 Called By: // 调用本函数的函数清单 Table Accessed: // 被访问的表(此项仅对于牵扯到数据库操作的程序) Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序) Input: // 输入参数说明,包括每个参数的作// 用、取值说明及参数间关系。 Output: // 对输出参数的说明。 Return: // 函数返回值的说明 Others: // 其它说明 ********************************************/
2-4: Comment while writing code, modify the code and modify the corresponding comments at the same time to ensure the consistency of comments and code. Comments that are no longer useful are deleted.
2-5: The content of the annotation must be clear and concise, and the meaning must be accurate to prevent ambiguity in the annotation. Explanation: Wrong comments are not only unhelpful but harmful.
2-6: Comments should be close to the code they describe. Comments on the code should be placed above or to the right (comments on a single statement) adjacent to them. They should not be placed below, such as above. It needs to be separated from the code above by a blank line.
Example: The following example does not comply with the specification.
Example 1:
/* get replicate sub system index and net indicator */ repssn_ind = ssn_data[index].repssn_index; repssn_ni = ssn_data[index].ni;
Example 2:
repssn_ind = ssn_data[index].repssn_index; repssn_ni = ssn_data[index].ni; /* get replicate sub system index and net indicator */
should be written as follows
/* get replicate sub system index and net indicator */ repssn_ind = ssn_data[index].repssn_index; repssn_ni = ssn_data[index].ni;
2-7: For all variables and constants with physical meaning, If its naming is not sufficiently self-explanatory, it must be annotated to explain its physical meaning when it is declared. Comments for variables, constants, and macros should be placed adjacent to or to the right above them.
Example:
/* active statistic task number */ #define MAX_ACT_TASK_NUMBER 1000 #define MAX_ACT_TASK_NUMBER 1000 /* active statistic task number */
2-8: Data structure declaration (including arrays, structures, classes, enumerations, etc.), if its naming is not fully self-annotated, it must be commented. Comments on the data structure should be placed adjacent to it, not below it; comments on each field in the structure should be placed to the right of this field.
Example: The enumeration/data/union structure can be described in the following form.
/* sccp interface with sccp user primitive message name */ enum SCCP_USER_PRIMITIVE { N_UNITDATA_IND, /* sccp notify sccp user unit data come */ N_NOTICE_IND, /* sccp notify user the No.7 network can not */ /* transmission this message */ N_UNITDATA_REQ, /* sccp user’s unit data transmission request */ };
2-9: Global variables should have more detailed comments, including descriptions of their functions, value ranges, which functions or procedures access them, and precautions when accessing them.
Example:
/* The ErrorCode when SCCP translate */ /* Global Title failure, as follows */ // 变量作用、含义 /* 0 - SUCCESS 1 - GT Table error */ /* 2 - GT error Others - no use */ // 变量取值范围 /* only function SCCPTranslate() in */ /* this modual can modify it, and other */ /* module can visit it through call */ /* the function GetGTTransErrorCode() */ // 使用方法 BYTE g_GTTranErrorCode;
2-10: Comments are indented in the same way as the content they describe.
Description: It can make the program layout neat and facilitate the reading and understanding of comments. Example: The following example is not neatly typed, making it slightly inconvenient to read.
void example_fun( void ) { /* code one comments */ CodeBlock One /* code two comments */ CodeBlock Two }
should be changed to the following layout.
void example_fun( void ) { /* code one comments */ CodeBlock One /* code two comments */ CodeBlock Two }
2-11: Avoid inserting comments in the middle of a line of code or expression.
Note: Unless necessary, comments should not be inserted in the middle of code or expressions, otherwise it will easily make the code less understandable.
2-12: Make the code self-commenting by correctly naming functions or procedures, variables, structures, etc. and rationally organizing the structure of the code.
Note: Clear and accurate naming of functions, variables, etc. can increase code readability and reduce unnecessary comments.
2-13: Comment at the function and intent level of the code to provide useful and additional information.
说明:注释的目的是解释代码的目的、功能和采用的方法,提供代码以外的信息,帮助读者理解代码,防止没必要的重复注释信息。
示例:如下注释意义不大。
/* if receive_flag is TRUE */ if (receive_flag)
而如下的注释则给出了额外有用的信息。
/* if mtp receive a message from links */ if (receive_flag)
2-14:在程序块的结束行右方加注释标记,以表明某程序块的结束。
说明:当代码段较长,特别是多重嵌套时,这样做可以使代码更清晰,更便于阅读。示例:参见如下例子。
if (…) { // program code while (index < MAX_INDEX) { // program code } /* end of while (index < MAX_INDEX) */ // 指明该条while 语句结束 } /* end of if (…)*/ // 指明是哪条if 语句结束
2-15:注释格式尽量统一,建议使用“/*…… */”。
2-16:注释应考虑程序易读及外观排版的因素,使用的语言若是中、英兼有的,建议多使用中文,除非能用非常流利准确的英文表达。
说明:注释语言不统一,影响程序易读性和外观排版,出于对维护人员的考虑,建议使用中文。
更多编程相关知识,请访问:编程教学!!
The above is the detailed content of In a C program, can a comment statement only be located after a statement?. For more information, please follow other related articles on the PHP Chinese website!