SoFunction
Updated on 2025-04-06

A brief discussion on the comment style summary in C language

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

/* comment*/

Another is single-line comment symbols:

// comment

When I was a student, I usually chose the latter. At that time, the number of encodings was very limited. Even the IDE used for simple paragraph annotations also supported batch addition of single-line comments. In encoding, the keyboard operation is simpler when commenting with simple single-line comments.

However, after working, after contacting the corresponding coding specifications, I basically gave up the single-line comment method for commenting in C language, and at most I only used it simply during debugging.

In fact, single-line comments are borrowed from C++ and are considered to be C++-style comments. This method sometimes brings certain differences in C language, and even introduces difficult-to-observe bugs. Let me summarize the two more typical methods I have seen from the Internet or books.

Example 1:

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

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

E:\WorkSpace\01_Programming Language\01_C Language\exp_26>gcc exp_26.c

E:\WorkSpace\01_Programming Language\01_C Language\exp_26>a

value of a: 123

The code is actually very simple. The first numerical modification to a is actually a useless redundant code. If you find similar problems, you may perform a simple blocking and modify the code as follows:

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

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

E:\WorkSpace\01_Programming Language\01_C Language\exp_26>gcc exp_26.c

E:\WorkSpace\01_Programming Language\01_C Language\exp_26>a

value of a: 0

This result is often beyond many people's expectations, because I found that the result is no longer 123! In fact, the reason is that there is a continuation symbol at the end of the blocked line. This allows the effect of the comment to continue to the next line. In fact, many compilers will give prompts with relatively accurate judgments in this regard. For example, the VIM I am using recently can prompt that the second line has also been commented out through color changes. Source Insight, which is good at semantic analysis, has not done a good job in this regard. I wonder if there have been any improvements in the latest V4 version.

Example 2:

#include""
 
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 "C Expert Programming". According to the introduction in the book, the three lines related to the assignment operation of c represent a/b in C language and a in C++. However, perhaps because the book was written earlier, this statement is obviously invalid in my own machine and software. Even in C language, the above expression means c = a. However, the readability of the program does give us a considerable challenge. I specifically paid attention to the editor's recognition of this. In this link, VIM and Source Insight both recognize accurately. It is worth mentioning that if the two examples in the previous examples are used in NotePad++, the editor's prompts are all accurate.

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

E:\WorkSpace\01_Programming Language\01_C Language\exp_27>gcc exp_27.c

E:\WorkSpace\01_Programming Language\01_C Language\exp_27>a

value of c:123

From this point of view, it is still reasonable to require that many embedded coding specifications cannot use C++ single-line annotation style annotation method. Although it brings some inconvenience, it can indeed avoid the occurrence of some minor problems.

The above brief summary of the annotation style in C language is all the content I share with you. I hope you can give you a reference and I hope you can support me more.