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

Example Analysis of Notes and matters needing attention in C language

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the C language comments and precautions in the example analysis, has a certain reference value, interested friends can refer to, I hope you read this article after a great harvest, the following let Xiaobian take you to understand.

Introduction to C Language Notes:

Comments should appear in three places

file header

function header

Comments mixed with code inside function bodies

For comments on file headers list at least:

Copyright notice, version number, file creation date, author, content/functionality, relationship to other files, modification log, etc.

Function header comments require at least:

Function function, input/output parameters, return value, calling/called relationship, etc.

C language annotation examples

Comments are deleted during precompilation, but the deletion is not literal deletion, but replacement, the comment is replaced, essentially replaced with spaces.

Take the following code as an example:

#include int main() { int /* */ i; //exact char *s = "abcdefgh //hijklmn"; //correct//Is it a\ valid comment? //correct in/* */t j; //error return 0; }

The sentence in t j cannot be compiled because it has been replaced with spaces.

If C-style comments are nested, then/* always matches the nearest */, and the second/* is not considered a comment symbol, so the first/* matches the first */, leaving a */:

Some notes to note

Annotations should be accurate and understandable, otherwise there is ambiguity. Bad comments are misleading and not conducive to code maintenance.

Modify comments while writing code, modify corresponding comments while modifying code, and ensure consistency between comments and code. Comments that are no longer useful should be deleted in time.

Comments are "hints" to the code, so comments should be simple and straightforward, because too many comments can be confusing.

Global data (global variables, constant definitions, etc.) must be commented on. Comments clearly define the purpose of global data.

Comments should be located adjacent to the code being described, either on the same line as the statement or one line above the statement, but not one line below the statement, because it does not conform to human reading habits.

When the code is long, especially when it is nested, you should add comments at the end of some paragraphs to make it easier to read.

Indent comments to be consistent with the indentation of the code, so that the code will be more beautiful, readable.

Comments on code snippets should focus on "why" rather than "how," explaining the intent of the code.

The unit of the value must be commented.

Comment on the scope of variables, especially parameters.

Comment on a series of numerical numbers, especially when writing low-level drivers.

Avoid inserting comments in the middle of a line of code or expression.

In complex functions, comments need to be added after the end of branch statements and loop statements to facilitate the distinction between branches or loop bodies.

Thank you for reading this article carefully. I hope that Xiaobian's "Example Analysis of Comments and Precautions in C Language" will be helpful to everyone. At the same time, I hope that everyone will support you a lot and pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!

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: 213

*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