Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summary of Annotation methods in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "Summary of Annotation methods in C language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

/ * comment*/

The other is a single-line comment symbol:

/ / comment

I usually chose the latter when I was a student, when the amount of coding was very limited, and even the IDE used for simple small paragraph comments supported the batch addition of single-line commentators. In coding, the operation of the keyboard is easier when simple single-line comments are commented.

However, after I came into contact with the corresponding coding specifications after my work, I basically gave up the method of single-line annotations for C language annotations, and at most I only used them simply when debugging.

In fact, single-line comments are borrowed from C++, which can be regarded as a C++-style annotation. This approach sometimes brings some differences in C language, and even introduces imperceptible Bug. List and summarize two typical ways that I have seen 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 result of the compilation of the code is as follows:

E:\ WorkSpace1_ programming language 1C language\ exp_26 > gcc exp_26.c

E:\ WorkSpace1_ programming language 1C language\ exp_26 > a

Value of a: 123

The code is actually very simple, the first numerical modification to an is actually a useless redundant code. If you find a similar problem, you may do a simple shield and modify the code 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 result of the compilation of the code is as follows:

E:\ WorkSpace1_ programming language 1C language\ exp_26 > gcc exp_26.c

E:\ WorkSpace1_ programming language 1C language\ exp_26 > a

Value of a: 0

This result is often unexpected to many people, because the result is no longer 123! In fact, the reason is that there is a continuation symbol at the end of the shielded line of code. This allows the comment to continue to the next line. In fact, many compilers will give a relatively accurate hint in this respect. For example, the VIM I have been using recently can indicate that the second line has been commented out by color change. Source Insight, which is good at semantic analysis, is not good at this, and I don't know if it has been improved in the latest V4 version.

Example 2:

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

This example is extracted from "C expert programming". According to the introduction in the book, three lines related to c's assignment operation represent a big b in C language and an in C++. However, it may be that the book was written earlier, and this statement is obviously not true in terms of my machine and software. Even in the C language, the above expression represents c = a. However, the readability of the program does give us a big challenge. I paid special attention to the editor's recognition of this, and in this section, VIM and Source Insight are all identified accurately. It is worth mentioning that if you use NotePad + + in the previous two examples, the editor prompts are all accurate.

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

E:\ WorkSpace1_ programming language 1C language\ exp_27 > gcc exp_27.c

E:\ WorkSpace1_ programming language 1C language\ exp_27 > a

Value of c:123

From this point of view, many embedded coding specifications require that C++ single-line comments can not be used in the style of comments has a certain degree of truth. Although it has brought some inconvenience, it can really avoid some minor problems.

This is the end of the summary of annotation methods in C language. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report