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

How to write C++ Code correctly

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to write C++ code correctly". 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!

Typesetting

(1) the program block should be written in indentation style, and the number of indented spaces is 4. The code in the statements such as the beginning of the function or process, the definition of the structure and the loop and judgment should all adopt the indentation style, and the situation handling statement under the case statement should also comply with the sentence indentation requirements.

Note: since the number of spaces automatically indented by each IDE text editor may be different, it is recommended to manually tap 4 space keys when indenting.

(2) between relatively independent program blocks, blank lines must be added after the variable description.

Example: the following example does not conform to the specification.

If (! valid_ni (ni))

{

... / / program code

}

Repssn_ind = ssn_ data [index] .repssn _ index

Repssn_ni = ssn_ data [index] .ni

It should be written as follows:

If (! valid_ni (ni))

{

... / / program code

}

Repssn_ind = ssn_ data [index] .repssn _ index

Repssn_ni = ssn_ data [index] .ni

(3) longer statements (such as loops, judgments, or functions, etc.) (> 80 characters) should be written in multiple lines, long expressions should be divided into new lines at the low priority operator, the operator should be placed at the beginning of the new line, and the divided new lines should be indented properly to make the typesetting neat and the sentence readable.

Example:

Perm_count_msg.head.len = NO7_TO_STAT_PERM_COUNT_LEN

+ STAT_SIZE_PER_FRAM * sizeof (_ UL)

For (I = 0, j = 0; (I

< BufferKeyword[word_index].word_length) && (j < NewKeyword.word_length); i++, j++) n7stat_flash_act_duration( stat_item, frame_id *STAT_TASK_CHECK_NUMBER + index, stat_object ); (4)不允许把多个短语句写在一行中, 即一行只写一条语句。 示例: 如下例子不符合规范。 rect.length = 0; rect.width = 0; 应如下书写 rect.length = 0; rect.width = 0; (5)if、for、do、while、case、switch、default等语句自占一行, 且if、for、do、while等语句的执行语句部分无论多少都要加括号{}。 示例: 如下例子不符合规范。 if (pUserCR == NULL) return; 应如下书写: if (pUserCR == NULL) { return; } (6)程序块的分界符(如C/C++ 语言的大括号'{' 和'}' )应各独占一行并且位于同一列, 同时与引用它们的语句左对齐。在函数体的开始、类的定义、结构的定义、枚举的定义以及if、for、do、while、switch、case 语句中的程序都要采用如上的缩进方式。示例: 如下例子不符合规范。 for (...) { ... // program code } if (...) { ... // program code } void example_fun( void ) { ... // program code } 应如下书写。 for (...) { ... // program code } if (...) { ... // program code } void example_fun( void ) { ... // program code } (7)在两个以上的关键字、变量、常量进行对等操作时, 它们之间的操作符之前、之后或者前后要加空格; 进行非对等操作时, 如果是关系密切的立即操作符(如->

), no spaces should be added after. Description: the purpose of writing code in this loose way is to make the code clearer. Because the clarity caused by leaving spaces is relative, there is no need to leave spaces in sentences that are already very clear. If the statement is clear enough, there is no need to add spaces on the inside of parentheses (that is, after the left parentheses and in front of the right parentheses), and there is no need to add spaces between multiple parentheses, because parentheses are already the clearest symbol in the Cpicrates + language. In long sentences, if you need to add a lot of spaces, then you should keep the whole clear, but not add spaces in the part. Do not leave more than two spaces in a row when leaving spaces for operators. Example: (1) commas and semicolons only add spaces after them.

Int a, b, c

(2) comparison operator, assignment operator "=", "+ =", arithmetic operator "+", "%", logical operator "&", "&", bit field operator ",". No spaces before and after.

P-> id = pid; / / "- >" without spaces before and after the pointer

(5) A space should be added between if, for, while, switch, etc., and the following parentheses to make if and other keywords more prominent and obvious.

If (a > = b & & c > d)

This is the end of the content of "how to write C++ Code correctly". Thank you for 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

Internet Technology

Wechat

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

12
Report