A brief summary of annotation style in C Language

  • 2020-06-23 01:29:19
  • OfStack

There are two common annotation styles in C language. One is to annotate a piece of code through the following mode:

/* comment*/

The other is a one-line comment symbol:

// comment

When I was a student, I usually chose the latter. At that time, the amount of code was limited, and IDE, which was used for simple small paragraph comments, also supported batch adding single-line comments. In coding, simple one-line comments make the keyboard one point easier to comment on.

However, after working with the corresponding coding specification, I basically abandoned the single-line comment method for the C language annotation and only used it for debugging.

In fact, single-line comments are borrowed from C++, which is the C++ style of comments. This approach can sometimes lead to 1-point differences in C, and even the introduction of Bug, which is hard to detect. Summarize 1 in two typical ways that I've read about myself on the Internet or in books.

Example 1:


#include"stdio.h"
 
int main(void)
{
    int a = 0;
 
    a += 5; \
    a = 123;
 
    printf("value of a: %d\n",a);
 
    return 0;
}

The compilation and operation results of the code are as follows:

E:\WorkSpace\01_ programming language \01_C \exp_26 > gcc exp_26.c

E:\WorkSpace\01_ programming language \01_C \exp_26 > a

value of a: 123

The code is pretty simple, and the first numerical change to a is a useless piece of redundant code. If similar problems are found, 1 simple mask may be performed, and the modified code is as follows:


#include"stdio.h"
 
int main(void)
{
    int a = 0;
 
    //a += 5; \
    a = 123;
 
    printf("value of a: %d\n",a);
 
    return 0;
}

The compilation and operation results of the code are as follows:

E:\WorkSpace\01_ programming language \01_C \exp_26 > gcc exp_26.c

E:\WorkSpace\01_ programming language \01_C \exp_26 > a

value of a: 0

This result will surprise many people most of the time, because the result is no longer 123! In fact, the reason is that there is an extra symbol at the end of the masked line. This makes the comment work until the next line. In fact, many compilers are relatively accurate in this regard, such as VIM, which I've been using recently, which indicates that line 2 is also commented out by color changes. However, Source Insight, which is good at semantic analysis, is not good at this aspect. It is unclear whether the latest version of V4 has improved this aspect.

Example 2:


#include"stdio.h"
 
int main(void)
{
    int a = 123;
    int b = 23;
    int c;
 
    c = a //*
           //*/b
    ;
   
    printf("value of c:%d",c);
 
    return 0;
}

This example is extracted from the C Expert Programming. According to the introduction in the book, the assignment of c refers to a/b in C and C++ to a. However, perhaps due to the early writing of the book, my machine and software, this is clearly not true. Even in C, the above expression means c = a. However, the readability of the program is a real challenge. I paid special attention to the recognition of 1 editor, in which both VIM and Source Insight were correctly identified. It is worth mentioning that both of the previous examples are accurate with NotePad ++.

The compilation and execution results of the code are as follows:

E:\WorkSpace\01_ programming language \01_C \exp_27 > gcc exp_27.c

E:\WorkSpace\01_ programming language \01_C \exp_27 > a

value of c:123

From this perspective, it makes sense that many embedded coding specifications require that C++ single-line annotation style should not be used. Although it brings the inconvenience of 1 fixed, it can indeed avoid the occurrence of 1 small problem.


Related articles: