C语言代码规范by宋学辉
前几天有新成员问到我关于代码规范的问题。关于代码规范,google就能找到很多的文档,我这也有一些。其实不同的环境, 不同的公司有不同的代码规范,没有一个统一的规定。但为了促进团队间代码的交流,和方便以后比赛时队友之间的代码查错,我整理了一份规范,请大家尽量遵守。内容如下:
1. 空行(4条规则) :
空行起着分隔程序段落的作用,空行使得程序的布局更加清晰。
【1-1】在函数内部局部变量定义结束之后处理语句之前要加空行。
【1-2】在每个函数定义结束之后都要加空行。参见示例1-1(a)。
【1-3】函数返回语句和其他语句之间使用空行分开。
【1-4】在一个函数体内,逻辑上密切相关的语句之间不加空行,其它地方应加空行分隔。
// 空行 void function1( ) { … } // 空行 void function2( ) { … } // 空行 void function3( ) { … } // 空行 while (condition) { … // 空行 if (condition) { … } else { … } // 空行 … }
2.代码行(5条规则)
【2-1】一行代码只做一件事情,如只写一条语句。这样的代码容易阅读。
【2-2】if、for、while、do 等语句自占一行,执行语句不得紧跟其后。不论执行语句有多少都要加{}表明是一个语句块。
【2-3】左花括号与语句同行(不要另起一行),右花括号要单独占一行。但是在do-while、struct和union及其后有‘;’的除外,要同在一行。
【2-4】switch语句中的每个case语句各占一行,当某个case语句不需要break语句最好加注释声明。
【2-5】并列的语句行应该按照字母顺序排序,如变量定义和switch中的case语句等。
if (condition) { //左括号不要另起一行 statement1; } else { statement2; } //右括号与相应语句对齐 do { statement3; } while();
3.代码行内的空格(7条规则)
【3-1】关键字之后要留空格。像int、const、case 等关键字之后至少要留一个空格,否则无法辨析关键字。
【3-2】函数名之后不要留空格,紧跟左括号“(”,以与关键字区别。例如:void calc(void);
【3-3】“,”之后要留空格,如function(x, y, z)。如果“;”不是一行的结束符号,其后要留空格。
如for( initialization; condition; update )。
【3-4】不要在单目运算符(如“!”、“~”、“++”、“--”、“&”)和其操作对象间加空格。
例如:!foo,++i,(long)getValue
【3-5】赋值操作符、比较操作符、算术操作符、逻辑操作符、位域操作符。
如“=”、“+=”、“>=”、“<=”、“+”、“*”、“%”、“&&”、“||”、“<<”,“^”等二元操作符的前后应当加空格。
【3-6】像“[]”、“.”、“->”这类操作符前后不加空格。
例如:big.bar,pFile->bar,big[bar]
【3-7】“(”的右边,“)”的左边不要加空格
//良好的代码 void func(int x, int y, int z); if(year >= 2000) if(a >= b && c <= d) for (i=0; i < 10; i++) x = a < b ? a : b; int *x = &y; array[5] = 0; a.function(); b->Function();
4.对齐(3条规则)
【4-1】程序的分界符“}”应独占一行并且位于同一列,同时与引用它的语句左对齐,分界符“{”不用独占一行紧跟在语句后。
【4-2】水平缩进每次使用四个空格即可(建议定义一个tab键为四个空格)。
【4-3】同属于一个语句块的代码对齐。
5.长行拆分(2条规则)
【5-1】代码行最大长度宜控制在70至80个字符以内。代码行不宜过长,否则不便于阅读,也不便于打印。
【5-2】长表达式要在低优先级操作符处拆分成新行,操作符放在新行之首(以便突出操作符)。拆分出的新行要进行适当的缩进,使排版整齐,语句可读。
if ((very_longer_variable1 >= very_longer_variable2) && (very_longer_variable3 <= very_longer_variable4) && (very_longer_variable5 <= very_longer_variable6)) { dosomething; } virtual CMatrix CMultiplyMatrix (CMatrix leftMatrix, CMatrix rightMatrix); for (very_long_initialization; very_long_condition; very_long_update) { dosomething; } if ((very_longer_variable1 >= very_longer_variable2) && (very_longer_variable3 <= very_longer_variable4) && (very_longer_variable5 <= very_longer_variable6)) { dosomething; }
6.标识符命名(2条规则)
标识符的命名规范很复杂很繁琐,这里只针对ACM比赛列两条命名的基本原则,对该项感兴趣的同学请自行查阅相关资料:
【6-1】含义清晰,不易混淆。
例如动态规划时的状态数组可以命名为dp[][]或是f[][],深度优先搜索的函数名可以命名为DFS,线段树的结构体可以命名为segTree
【6-2】不与其它模块、函数的命名空间相冲突。 避免用map、max、abs、sort等标识符命名函数,避免和库函数冲突。
7.常量(3条规则)
C 语言可以用const 来定义常量,也可以用#define 来定义常量。但是前者比后者有更多的优点:
(1) const 常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查。而对后者只进行字符替换,没有类型安全检查,并且在字符替换过程中可能会产生意料不到的错误。
(2) 有些集成化的调试工具可以对const 常量进行调试,但是不能对宏常量进行调试。
【7-1】尽量使用const定义常量替代宏定义常量。
【7-2】用常量定义数据范围,避免不小心把范围写错后修改一堆数组的大小,这里建议使用define。例如:
#define M 1000 int pre[M], next[M], dp[M][M], a[M][M]; vector <int> edge[M];
【7-3】函数宏的每个参数都要括起来。
例如:#define WEEKS_TO_DAYS(w) (w*7)
应该写成:#define WEEKS_TO_DAYS(w) ((w)*7)
这样在翻译totalDays=WEEKS_TO_DAYS(1+2)时,才能够正确地翻译成:(1+2)*7;否则将错误地翻译成1+2*7。
建议这里使用内联函数。如:
inline int lowbit(int x) { return x & (-x); }
2010年4月05日 21:49
写变量的时候最好用自己能记住的变量名,像单字符的a,b,c,d,e,f,g可以少量的用,最后使用短的英文单词或者英文缩写,英文不好的同学也可以用汉语拼音缩写,比如以前有学长就用ljb表示“邻接表”,最近我也看到用bjd表示“比较大”,这些也是可以的。总之大原则就是能“望文生义”。
by 郭琦
2025年1月06日 14:39
A very Wonderful blog. We believe that you have a busy, active lifestyle and also understand you need marijuana products from time to time. We’re ICO sponsored and in collaboration with doctors, deliveries, storefronts and deals worldwide, we offer a delivery service – rated
2025年1月06日 15:55
The wool jackets are back in style! These timeless pieces have made a comeback in the fashion industry, with their warm and cozy textures and classic designs. From oversized to tailored, wool jackets are versatile and perfect for layering in any season. Get ready to embrace the wool jacket trend.
2025年1月06日 15:59
Natalie, I think you’re been taxed.
2025年1月06日 16:18
It is a scripting spoken market place.
2025年1月06日 16:21
Very best people messages are meant interests home. best man speach
2025年1月06日 16:36
Continually, when people manage have school children.
2025年1月06日 16:37
If you’re planning an extended comfortable and cost-effective trip.
2025年1月06日 16:48
It’s a good shame you don’t contain a give money button! I’d definitely give money Facebook or twitter team
2025年1月06日 17:09
I am only commenting to let topic to Kate.
2025年1月06日 17:11
Continually, when people manage children.
2025年1月06日 18:08
I am studying at a medical school and at the same time I work part-time in a cosmetology company. It is almost impossible for me to combine all this. Especially to devote enough time to study. Recently, we were asked an essay and I decided that it would be best and easiest to buy it, click this over here now, maybe it will be useful to you sometime.
2025年1月06日 18:09
One time neglecting your web site That found himself savings and will also be seeking out it really is which were found to educate yourself about to read the paper your the latest!
2025年1月06日 18:11
After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.
2025年1月06日 18:13
Another reason why that men attempt occupations hiring into my field is really because seriously is invaluable staying near home in the event that numerous vital internal send out appears. Nevertheless this is to speak about certainly nothing of their advantages engaging in case you have school children.
2025年1月06日 18:14
Immerse yourself in unrivaled sophistication.
2025年1月06日 18:15
Investment is one of the best ways to achieve financial freedom. For a beginner there are so many challenges you face. Its hard to know how to get started. Trading on the Cryptocurrency market has really been a life changer for me. I almost gave up on crypto at some point not until saw a recommendation on Elon musk successfully success story and I got a proficient trader/broker Mr Bernie Doran , he gave me all the information required to succeed in trading. I made more profit than I could ever imagine. I\'m not here to converse much but to share my testimony; I have made total returns of $20,500.00 from an investment of just $2000.00 within 1 week. Thanks to Mr Bernie I\'m really grateful,I have been able to make a great returns trading with his signals and strategies .I urge anyone interested in INVESTMENT to take bold step in investing in the Cryptocurrency Market, he can also help you RECOVER your lost Cryptocurrencies.
2025年1月06日 18:15
Hello Fella, What you ?came up with here absolutely have me wicked up to the last word, and I wanna say to you I rarely read the entire post of blogs as I often got sick and tired of the gibberish that is presented in the junkyard of the world wide web on a daily basis and I simply end up checking out the headlines and maybe the first lines or something like that. But your headline and the first few rows were exceptional and it right on the spot forced me to stay. So, I just wanna say: nice and rare job! Thanks, really.
2025年1月06日 18:19
Only doing diet and arm exercises. Exercise helps you tone your arms, but without dieting you will not be able to eliminate extra fat from your arm. You cannot lose arm-only fat (unless you have a liposuction of arms), you spend losing fat all over your body so that you lose fat from your arms, and this is only achieved with diet, you take a low-calorie diet, I recommend you follow the ketogenic diet.
2025年1月06日 18:26
Very best people messages are meant to charm allow honor toward groom and bride. Newbie speakers in front of excessive locations should usually our own gold colored dominate in presenting and public speaking, which is to be personal interests home. best man speach
2025年1月06日 18:27
Fly-by single-comment spammers may not be rewarded with do-follow backlinks or regarded, however they do opt in to your newsletter. Your loyal guests are are subsequently rewarded.
2025年1月06日 18:28
Recsport TV the leading football live streaming platform.\r\nRecsport TV is a comprehensive platform that provides the latest data on scores, fixtures, and football results from around the world. With its user-friendly interface and reliable sources, it is the perfect destination for football enthusiasts who want to quickly stay updated on the latest events in the football
2025年1月06日 18:30
Thanks for the tips you have provided here. One more thing I would like to mention is that laptop memory requirements generally increase along with other breakthroughs in the engineering. For instance, as soon as new generations of processors are introduced to the market, there is usually a matching increase in the shape demands of all computer system memory plus hard drive room. This is because the application operated by these cpus will inevitably boost in power to leverage the new engineering.
2025年1月06日 18:31
Just want to say your article is as astonishing. The clarity in your post is just nice and i can assume you are an expert on this subject. Fine with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please continue the gratifying work.