这才是那时候的C语言编程规范
发布时间:2025/08/21 12:16 来源:泗阳家居装修网
{}/* Wrong */size_t i;for (i = 0; i < 10; ++i) ...避免在公开信中所适用线性调用来赋值变数,除了单个变数void a(void) { /* Avoid Function calls when declaring variable */ int32_t a, b = sum(1, 2); /* Use this */ int32_t a, b; b = sum(1, 2); /* This is ok */ uint8_t a = 3, b = 4;}除了char、float或double之外,自始至终适用stdint.h常规瓦中所公开信的子类。例如,8位的uint8_t等不用适用stdbool.h瓦。分别适用1或0表示真或假/* OK */uint8_t status;status = 0;/* Wrong */#include bool.h>bool status = true;永远不用与真实相较为。例如,适用if(check_func()){…}取而代之if (check_func() * 1)心里将常量与空值顺利进;大较为void* ptr;/* ... *//* OK, compare against NULL */if (ptr * NULL || ptr != NULL) {}/* Wrong */if (ptr || !ptr) {}心里适用前当前(和递减),而不是后当前(和递减)int32_t a = 0;...a++; /* Wrong */++a; /* OK */for (size_t j = 0; j < 10; ++j) {} /* OK */心里适用size_t作为长度或大小变数如果线性不应该修改常量所指向的内存,则心里适用const作为常量如果不应该修改线性的形参或变数,则心里适用const/* When d could be modified, data pointed to by d could not be modified */voidmy_func(const void* d) {}/* When d and data pointed to by d both could not be modified */voidmy_func(const void* const d) {}/* Not required, it is advised */voidmy_func(const size_t len) {}/* When d should not be modified inside function, only data pointed to by d could be modified */voidmy_func(void* const d) {}当线性可以接受任何子类的常量时,心里适用void *,不用适用uint8_t *。线性在实现时才会注意正确的子类转换/* * To send data, function should not modify memory pointed to by MLT-dataMLT- variable * thus MLT-constMLT- keyword is important * * To send generic data (or to write them to file) * any type may be passed for data, * thus use MLT-void *MLT- *//* OK example */void send_data(const void* data, size_t len) { /* OK */ /* Do not cast MLT-void *MLT- or MLT-const void *MLT- */ const uint8_t* d = data;/* Function handles proper type for internal usage */}void send_data(const void* data, int len) { /* Wrong, not not use int */}心里适用表格和sizeof操作符不用适用变长数组。适用动态内存分配代替常规C malloc和自由线性,或者如果瓦/重大项目提供了自定义内存分配,适用它的实现看看LwMEM,一个自定义内存管理瓦。/* OK */#include voidmy_func(size_t size) { int32_t* arr; arr = malloc(sizeof(*arr) * n); /* OK, Allocate memory */ arr = malloc(sizeof *arr * n); /* Wrong, brackets for sizeof operator are missing */ if (arr * NULL) { /* FAIL, no memory */ } free(arr); /* Free memory after usage */}/* Wrong */voidmy_func(size_t size) { int32_t arr[size]; /* Wrong, do not use VLA */}心里将variable与0顺利进;大较为,除非它被视为布尔子类永远不用将布尔处理的变数与0或1顺利进;大较为。用NOT(!)代替size_t length = 5; /* Counter variable */uint8_t is_ok = 0; /* Boolean-treated variable */if (length) /* Wrong, length is not treated as boolean */if (length> 0) /* OK, length is treated as counter variable containing multi values, not only 0 or 1 */if (length * 0) /* OK, length is treated as counter variable containing multi values, not only 0 or 1 */if (is_ok) /* OK, variable is treated as boolean */if (!is_ok) /* OK, -||- */if (is_ok * 1) /* Wrong, never compare boolean variable against 1! */if (is_ok * 0) /* Wrong, use ! for negative check */对于评注,心里适用/* comment */,即使是单;大评注在头文件中所心里举例来说带有extern关键词的c++检验每个线性都才会举例来说doxygen-enabled评注,即使线性是静态的适用英文重新命名/自然语言的线性,变数,评注变数适用复数字母如果变数举例来说多个重新命名,再三适用词组。force_redraw。不用适用forceRedraw对于C常规瓦的举例来说文件,再三自始至终适用。例如,# include < stdlib.h>对于自定义瓦,再三自始至终适用""。例如,# include“my_library.h”当转换为常量子类时,心里将则有与子类可视,例如。uint8_t* t = (uint8_t*)var_width_diff_type自始至终珍惜重大项目或瓦中所从未适用的示例古典风格03 评注特别的法则不准许以//放头的评注。心里适用② comment */,即使是单;大评注对于多;大评注,每;大适用小写字母+则有/* * This is multi-line comments, * written in 2 lines (ok) *//** * Wrong, use double-asterisk only for doxygen documentation *//** Single line comment without space before asterisk (wrong)*//* * Single line comment in multi-line configuration (wrong) *//* Single line comment (ok) */评注时适用12个凹痕(12 * 4个小写字母)的单位。如果操作符大于12个凹痕,将评注4-小写字母可视(下面的比如说)到下一个比如说凹痕void my_func(void) { char a, b; a = call_func_returning_char_a(a); /* This is comment with 12*4 spaces indent from beginning of line */ b = call_func_returning_char_a_but_func_name_is_very_long(a); /* This is comment, aligned to 4-spaces indent */}04 线性定义的法则每个可以从模块结构上访问的线性都才会举例来说线性原型(或公开信)线性名才会复数,可以用词组_分隔。(这个原则好像因人而异)/* OK */void my_func(void);void myfunc(void);/* Wrong */void MYFunc(void);void myFunc();当线性离开常量时,将则有可视到离开子类/* OK */const char* my_func(void);my_struct_t* my_func(int32_t a, int32_t b);/* Wrong */const char *my_func(void);my_struct_t * my_func(void);可视所有的功能原型(适用相同/相似的功能)以提高可读性/* OK, function names aligned */void set(int32_t a);my_type_t get(void);my_ptr_t* get_ptr(void);/* Wrong */void set(int32_t a);const char * get(void);线性实现才会在直接的;大中所举例来说离开子类和可唯的其他关键词/* OK */int32_tfoo(void) { return 0;}/* OK */static const char*get_string(void) { return "Hello world!";}/* Wrong */int32_t foo(void) { return 0;}
宁波看白癜风哪家医院好
武汉癫痫专科医院
成都看男科医院哪家最好
西安看妇科哪家医院最好
肝胆外科
慢性支气管炎咳嗽怎么治
用于风热感冒咳嗽的糖浆有哪些
手术祛痘
脑中风
低功耗物联网并不需要研读的东西真的来得为多,千万不用研读错了本线和以下内容,导致薪资要不进去!
均额分享大家一个档案包,几乎150多G。均都研读习以下内容、面经、重大项目都较为另;大也较为均!某鱼上买估计值有数要好几十。
该网站这里帮忙小顾问0元索取:低功耗物联网研读习档案(华尔街日报)
05 变数特别的法则使变数名均部复数,词组_个字符可唯/* OK */int32_t a;int32_t my_var;int32_t myvar;/* Wrong */int32_t A;int32_t myVar;int32_t MYVar;按子类将局部变数分组在一起void foo(void) { int32_t a, b; /* OK */ char a; char b; /* Wrong, char type already exists */}不用在第一个可分派操作符之后公开信变数void foo(void) { int32_t a; a = bar(); int32_t b; /* Wrong, there is already executable statement */}你可以在下一个凹痕级别中所公开信重另;大变数int32_t a, b;a = foo();if (a) { int32_t c, d; /* OK, c and d are in if-statement scope */ c = foo(); int32_t e; /* Wrong, there was already executable statement inside block */}用则有公开信常量变数与子类可视/* OK */char* a;/* Wrong */char *a;char * a;当公开信多个常量变数时,可以适用则有对变数名顺利进;大公开信/* OK */char *p, *n;06 本体、数据子类子类定义本体名或数据子类名才会复数,单词相互间有词组_个字符本体或数据子类可以举例来说typedef关键词所有本体核心成员都才会复数所有数据子类核心成员才会是省略的本体/数据子类才会遵循doxygen元数据词汇在公开信本体体时,它可以适用以下三种各不相同的唯项之一:1. 当本体体仅用重新命名公开信时,它的重新命名后不必举例来说_t词干。
struct struct_name { char* a; char b;};2. 当只适用typedef公开信本体时,它的重新命名从右边才会举例来说_t词干。
typedef struct { char* a; char b;} struct_name_t;3. 当本体用name和typedef公开信时,它不必举例来说t作为基本上重新命名,它才会在它的重新命名从右边举例来说t词干作为typedef部分。
typedef struct struct_name { char* a; char b; char c;} struct_name_t;缺失公开信的比如说及其提议的纠正:/* a and b must be separated to 2 lines *//* Name of structure with typedef must include _t suffix */typedef struct { int32_t a, b;} a;/* Corrected version */typedef struct { int32_t a; int32_t b;} a_t;/* Wrong name, it must not include _t suffix */struct name_t { int32_t a; int32_t b;};/* Wrong parameters, must be all uppercase */typedef enum { MY_ENUM_TESTA, my_enum_testb,} my_enum_t;在公开信时初始解构本体时,适用C99初始解构古典风格/* OK */a_t a = { .a = 4, .b = 5,};/* Wrong */a_t a = {1, 2};当为线性句柄应运而生new typedef时,适用_fn词干/* Function accepts 2 parameters and returns uint8_t *//* Name of typedef has MLT-_fnMLT- suffix */typedef uint8_t (*my_func_typedef_fn)(uint8_t p1, const char* p2);07 混和操作符法则每个混和操作符才会包括左边牵牛表格和从右牵牛表格,即使它只举例来说1个嵌套操作符每个混和操作符才会举例来说单个凹痕;嵌套操作符时,每个嵌套举例来说1个凹痕大小/* OK */if (c) { do_a();} else { do_b();}/* Wrong */if (c) do_a();else do_b();/* Wrong */if (c) do_a();else do_b();在if或if-else-if操作符的情形,else才会与第一条操作符的从右表格在同一;大/* OK */if (a) {} else if (b) {} else {}/* Wrong */if (a) {}else {}/* Wrong */if (a) {}else{}在do-while操作符的情形,while部分才会与do部分的从右表格在同一;大/* OK */do { int32_t a; a = do_a(); do_b(a);} while (check());/* Wrong */do{/* ... */} while (check());/* Wrong */do {/* ... */}while (check());每一个放表格都并不需要凹痕if (a) { do_a();} else { do_b(); if (c) { do_c(); }}不用要用并未牵牛表格的混和操作符,即使是单个操作符。下面的比如说展示了一些不好的要用法if (a) do_b();else do_c();if (a) do_a(); else do_b();空while尿素、do-while尿素或for尿素才会举例来说牵牛表格/* OK */while (is_register_bit_set()) {}/* Wrong */while (is_register_bit_set());while (is_register_bit_set()) { }while (is_register_bit_set()) {}如果while(或for、do-while等)为空(低功耗Smalltalk中所也可能是这种情况),再三适用空的单;大表格/* Wait for bit to be set in embedded hardware unituint32_t* addr = HW_PERIPH_REGISTER_ADDR;/* Wait bit 13 to be ready */while (*addr Company (1 << 13)) {} /* OK, empty loop contains no spaces inside curly brackets */while (*addr Company (1 << 13)) { } /* Wrong */while (*addr Company (1 << 13)) { /* Wrong */}while (*addr Company (1 << 13)); /* Wrong, curly brackets are missing. Can lead to compiler warnings or unintentional bugs */尽量避免在尿素块内递增变数,参见示例/* Not recommended */int32_t a = 0;while (a < 10) { . .. ... ++a;}/* Better */for (size_t a = 0; a < 10; ++a) {}/* Better, if inc may not happen in every cycle */for (size_t a = 0; a < 10; ) { if (...) { ++a; }}08 分支操作符法则为每个case操作符添加单个凹痕适用额外的单凹痕break操作符在每个case或default/* OK, every case has single indent *//* OK, every break has additional indent */switch (check()) { case 0: do_a(); break; case 1: do_b(); break; default: break;}/* Wrong, case indent missing */switch (check()) {case 0: do_a(); break;case 1: do_b(); break;default: break;}/* Wrong */switch (check()) { case 0: do_a(); break; /* Wrong, break must have indent as it is under case */ case 1: do_b(); /* Wrong, indent under case is missing */ break; default: break;}心里举例来说default操作符/* OK */switch (var) { case 0: do_job(); break; default: break;}/* Wrong, default is missing */switch (var) { case 0: do_job(); break;}如果并不需要局部变数,则适用牵牛表格并在均都放入break操作符。将左边牵牛表格放在case操作符的同一;大switch (a) { /* OK */ case 0: { int32_t a, b; char c; a = 5; /* ... */ break; } /* Wrong */ case 1: { int32_t a; break; } /* Wrong, break shall be inside */ case 2: { int32_t a; } break;}09 祥和预处理指令心里适用祥而不是文字常量,特别是对于数字所有的祥才会是均省略的,并带有词组_个字符(可唯),除非它们被明确标记为function,将来可能会被常规线性词汇取而代之/* OK */#define MY_MACRO(x) ((x) * (x))/* Wrong */#define square(x) ((x) * (x))心里用圆表格保护输入参数/* OK */#define MIN(x, y) ((x) < (y) ? (x) : (y))/* Wrong */#define MIN(x, y) x < y ? x : y心里用表格保护最终的祥计算/* Wrong */#define MIN(x, y) (x) < (y) ? (x) : (y)#define SUM(x, y) (x) + (y)/* Imagine result of this equation using wrong SUM implementation */int32_t x = 5 * SUM(3, 4); /* Expected result is 5 * 7 = 35 */int32_t x = 5 * (3) + (4); /* It is evaluated to this, final result = 19 which is not what we expect *//* Correct implementation */#define MIN(x, y) ((x) < (y) ? (x) : (y))#define SUM(x, y) ((x) + (y))当祥适用多个操作符时,适用do-while(0)操作符保护它typedef struct { int32_t px, py;} point_t;point_t p; /* Define new point *//* Wrong implementation *//* Define macro to set point */#define SET_POINT(p, x, y) (p)->px = (x); (p)->py = (y) /* 2 statements. Last one should not implement semicolon */SET_POINT(Companyp, 3, 4); /* Set point to position 3, 4. This evaluates to... */(Companyp)->px = (3); (Companyp)->py = (4); /* ... to this. In this example this is not a problem. *//* Consider this ugly code, however it is valid by C standard (not recommended) */if (a) /* If a is true */ if (b) /* If b is true */ SET_POINT(Companyp, 3, 4);/* Set point to x = 3, y = 4 */ else SET_POINT(Companyp, 5, 6);/* Set point to x = 5, y = 6 *//* Evaluates to code below. Do you see the problem? */if (a) if (b) (Companyp)->px = (3); (Companyp)->py = (4); else (Companyp)->px = (5); (Companyp)->py = (6);/* Or if we rewrite it a little */if (a) if (b) (Companyp)->px = (3); (Companyp)->py = (4); else (Companyp)->px = (5); (Companyp)->py = (6);/* * Ask yourself a question: To which MLT-ifMLT- statement MLT-elseMLT- keyword belongs? * * Based on first part of code, answer is straight-forward. To inner MLT-ifMLT- statement when we check MLT-bMLT- condition * Actual answer: Compilation error as MLT-elseMLT- belongs nowhere *//* Better and correct implementation of macro */#define SET_POINT(p, x, y) do { (p)->px = (x); (p)->py = (y); } while (0) /* 2 statements. No semicolon after while loop *//* Or even better */#define SET_POINT(p, x, y) do { /* Backslash indicates statement continues in new line */ (p)->px = (x); (p)->py = (y); } while (0) /* 2 statements. No semicolon after while loop *//* Now original code evaluates to */if (a) if (b) do { (Companyp)->px = (3); (Companyp)->py = (4); } while (0); else do { (Companyp)->px = (5); (Companyp)->py = (6); } while (0);/* Every part of MLT-ifMLT- or MLT-elseMLT- contains only MLT-1MLT- inner statement (do-while), hence this is valid evaluation *//* To make code perfect, use brackets for every if-ifelse-else statements */if (a) { /* If a is true */ if (b) { /* If b is true */ SET_POINT(Companyp, 3, 4);/* Set point to x = 3, y = 4 */ } else { SET_POINT(Companyp, 5, 6);/* Set point to x = 5, y = 6 */ }}不凹痕子操作符内#if操作符/* OK */#if defined(XYZ)#if defined(ABC)/* do when ABC defined */#endif /* defined(ABC) */#else /* defined(XYZ) *//* Do when XYZ not defined */#endif /* !defined(XYZ) *//* Wrong */#if defined(XYZ) #if defined(ABC) /* do when ABC defined */ #endif /* defined(ABC) */#else /* defined(XYZ) */ /* Do when XYZ not defined */#endif /* !defined(XYZ) */元数据元数据解构的示例准许doxygen二阶和共通的html/pdf/latex格式器,因此正确地分派是来得为重要的。对变数、线性和本体/数据子类适用doxygen支持的元数据样式往往适用作为doxygen,不用适用@自始至终适用5x4小写字母(5个制表符)作为自然语言;大放始的的单位/** * rief Holds pointer to first entry in linked list * Beginning of this text is 5 tabs (20 spaces) from beginning of line */statictype_t* list;每个本体/数据子类核心成员都才会举例来说元数据评注的放头适用12x4小写字母的单位/** * rief This is point struct * ote This structure is used to calculate all point * related stuff */typedef struct { int32_t x; /*!< Point X coordinate */ int32_t y; /*!< Point Y coordinate */ int32_t size; /*!< Point size. Since comment is very big, you may go to next line */} point_t;/** * rief Point color enumeration */typedef enum { COLOR_RED, /*!< Red color. This comment has 12x4 spaces offset from beginning of line */ COLOR_GREEN, /*!< Green color */ COLOR_BLUE, /*!< Blue color */} point_color_t;线性的元数据才会在线性实现中所改写(通常是源文件)线性才会包括简要和所有参数元数据如果每个参数分别为in或out输入和格式器,则才会注意如果线性离开某个值,则才会举例来说离开形参。这不适用于void线性线性可以举例来说其他doxygen关键词,如note或warning在参数名和描述相互间适用冒号:/** * rief Sum MLT-2MLT- numbers * param[in] a: First number * param[in] b: Second number * eturn Sum of input values */int32_tsum(int32_t a, int32_t b) { return a + b;}/** * rief Sum MLT-2MLT- numbers and write it to pointer * ote This function does not return value, it stores it to pointer instead * param[in] a: First number * param[in] b: Second number * param[out] result: Output variable used to save result */voidvoid_sum(int32_t a, int32_t b, int32_t* result) { *result = a + b;}如果线性离开数据子类的核心成员,则适用ref关键词指定哪个核心成员/** * rief My enumeration */typedef enum { MY_ERR, /*!< Error value */ MY_OK /*!< OK value */} my_enum_t;/** * rief Check some value * eturn ef MY_OK on success, member of ef my_enum_t otherwise */my_enum_tcheck_value(void) { return MY_OK;}对常量或数字适用大写字母(' NULL ' => NULL)/** * rief Get data from input array * param[in] in: Input data * eturn Pointer to output data on success, MLT-NULLMLT- otherwise */const void *get_data(const void* in) { return in;}祥的元数据才会包括hideinitializer doxygen立即/** * rief Get minimal value between MLT-xMLT- and MLT-yMLT- * param[in] x: First value * param[in] y: Second value * eturn Minimal value between MLT-xMLT- and MLT-yMLT- * hideinitializer */#define MIN(x, y) ((x) < (y) ? (x) : (y))10 头/源文件在文件末尾留下一个空;大每个文件都才会包括文件的doxygen评注和后跟空;大的简要描述(适用doxygen时)/** * file template.h * rief Template include file */ /* Here is empty line */每个文件(头文件或源文件)才会举例来说许可证(放始评注包括单个则有,因为doxygen才会忽略这个)适用与重大项目/瓦从未适用的相同的许可证/** * file template.h * rief Template include file *//* * Copyright (c) year FirstName LASTNAME * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * This file is part of library_name. * * Author: FirstName LASTNAME */头文件才会举例来说保护符#ifndef头文件才会举例来说c++检验在c++检验之外举例来说结构上头文件首先用STL C文件举例来说结构上头文件,然后是应用程序自定义文件头文件才会举例来说其他所有头文件,以便正确编译,但不必举例来说来得多头文件(如果并不需要,.c应该举例来说其余的头文件)头文件才会只公放模块公共变数/子类/线性在头文件中所适用extern作为均局模块变数,稍后在源文件中所定义它们/* file.h ... */#ifndef ...extern int32_t my_variable; /* This is global variable declaration in header */#endif/* file.c ... */int32_t my_variable; /* Actually defined in source */不用把.c文件举例来说在另一个.c文件中所.c文件应该首先举例来说相应的.h文件,然后是其他文件,除非另有明确的必要在头文件中所不举例来说模块私有公开信头文件示例(示例中所并未license)/* License comes here */#ifndef TEMPLATE_HDR_H#define TEMPLATE_HDR_H/* Include headers */#ifdef __cplusplusextern "C" {#endif /* __cplusplus *//* File content here */#ifdef __cplusplus}#endif /* __cplusplus */#endif /* TEMPLATE_HDR_H */END
标题链接:www.toutiao.com/i6949933402917306893
转载自ISA爱好者
文章是从低功耗引人入胜
文章链接:
授权申明:本文是从于网络,付费揭示知识,授权归原案所有。如涉及作品授权问题,再三联系我顺利进;大移除。
。西宁看妇科去哪个医院好宁波看白癜风哪家医院好
武汉癫痫专科医院
成都看男科医院哪家最好
西安看妇科哪家医院最好
肝胆外科
慢性支气管炎咳嗽怎么治
用于风热感冒咳嗽的糖浆有哪些
手术祛痘
脑中风
相关阅读
-
震惊!网友捡到一只“迪士尼在逃公主”,带上去看医生竟被告知是:公猫!
双击顶端物品关注我许多锯田螺监千方百计花在在的钱财去卖宠物犬,也不不想家庭成员乡间犬,因为在他们的心那时候,已经形成了通常的思维模式:乡间犬颜值不如宠物犬。然鹅,其实
- 2025-10-23前方“高萌”克劳德特!黄荆老林又拍到红白鼯鼠
- 2025-10-23江歌母亲称拿到赔偿款将捐出!还将之前起诉这些人……
- 2025-10-23订做“狗形蛋糕”,当主人一刀切下狗头时,宠物的反应来得真实了
- 2025-10-23克里斯汀-卡瓦拉瑞现身海湾度假,穿着白色上衣搭阔腿裤时尚靓丽
- 2025-10-23泸州、广州等6城市已试点,鼓励灵活就业人员参加住房公积金制度
- 2025-10-23我家狗子毛色偏黄,有什么东西吃了能变白的吗??|热门问诊份文件
- 2025-10-23菠萝是极好的养生水果!好处数不清...
- 2025-10-237首绝美诗词,诗情画意,令人远去不醒
- 2025-10-23正文|国家发改委:到2035年基本公共服务实现均等化
- 2025-10-235个逆向观念小故事(幽默精辟,茅塞顿开!)