C代码规范

C代码规范

写一下,在公司里约定的规范,给大家做下参考。

工程注释模板

  • keil下注意保留文件最后空行,否者编译出现警告
  • 语句完整性要求:
    • if语句需带有花括号完整包含条件语句
    • 不带参数的函数使用void说明禁止参数表为空
  • 同行注释使用: /**< 注释语句*/
  • 尽可能的使用/* 注释 */ 对执行逻辑的注释,而不是//
  • 空格要求:声明或者定义函数时,其括号内参数以 数据类型+空格+参数名+逗号+空格......

C文件模板

/**
 *  @file xxx.c
 *
 *  @date 2021-02-7
 *
 *  @author aron566
 *
 *  @copyright None.
 *
 *  @brief 
 *
 *  @details 1、
 *
 *  @version V1.0
 */
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "xx.h"
/* Use C compiler ----------------------------------------------------------*/
#ifdef __cplusplus ///< use C compiler
extern "C" {
#endif
/** Private typedef ----------------------------------------------------------*/
/** Private macros -----------------------------------------------------------*/
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/
/** Private variables --------------------------------------------------------*/

/** Private function prototypes ----------------------------------------------*/

/** Private user code --------------------------------------------------------*/
/**
 * @defgroup modulename $FILE_FNAME$
 * @{
 */
static void xx(void);
/** @}*/

/** Private application code -------------------------------------------------*/
/*******************************************************************************
*
*       Static code
*
********************************************************************************
*/
/**
  ******************************************************************
  * @brief   
  * @param   [in]None
  * @return  TRUE  成功
  * @return  FALSE 失败
  * @author  aron566
  * @version V1.0
  * @date    2021-01-25
  ******************************************************************
  */
/** Public application code --------------------------------------------------*/
/*******************************************************************************
*
*       Public code
*
********************************************************************************
*/
#ifdef __cplusplus ///<end extern c
}
#endif
/******************************** End of file *********************************/

h文件模板

/**
 *  @file xxx.h
 *
 *  @date 2021-01-25
 *
 *  @author aron566
 *
 *  @brief 
 *  
 *  @version V1.0
 */
#ifndef $FILE_FNAME$%c
#define $FILE_FNAME$
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< need definition of uint8_t */
#include <stddef.h> /**< need definition of NULL    */
#include <stdbool.h>/**< need definition of BOOL    */
#include <stdio.h>  /**< if need printf             */
#include <stdlib.h>
#include <string.h>
#include <limits.h> /**< need variable max value    */
/* Use C compiler ----------------------------------------------------------*/
#ifdef __cplusplus ///< use C compiler
extern "C" {
#endif
/** Private includes ---------------------------------------------------------*/

/** Private defines ----------------------------------------------------------*/
/**
 * @name 分组说明文字
 * @{
 */
#define xxx "sss"
/** @}*/
/** Exported typedefines -----------------------------------------------------*/
/*枚举*/
typedef enum
{
    XXXXX = 0,
    
}XX_Typedef_t
    
/** 数据结构体*/
typedef struct
{
    xx; /**< 说明 */
}xxx_Typedef_t;

/** Exported constants -------------------------------------------------------*/

/** Exported macros-----------------------------------------------------------*/
/** Exported variables -------------------------------------------------------*/
/** Exported functions prototypes --------------------------------------------*/

#ifdef __cplusplus ///<end extern c
}
#endif
#endif
/******************************** End of file *********************************/

函数注释

/**
  ******************************************************************
  * @brief   %2
  * @param   [in]None
  * @return  TRUE  成功
  * @return  FALSE 失败
  * @author  aron566
  * @version v1.0
  * @date    2021-5-28
  ******************************************************************
  */
static void xxx(void)
{
    return;
}

函数或变量命名方式

  • 函数:大驼峰+下划线方式:void Get_Val(void) 在Linux中是以小写+下划线方式:void get_val(void);

  • 宏定义枚举:皆使用大写

  • 重命名: 大写字母_Typedef_t 如:typedef struct Opt_Handle Opt_Handle_Typedef_t

文件编码

  • 编码是使用UTF-8编码无BOM

对齐方式

  • TAB键使用2空格缩进

优化

  • 全局变量尽可能少,无需对外的全局变量皆使用static关键字声明
  • 需对外的函数接口函数或者类型定义声明到头文件,否则应为对应c文件的私有
  • 传参,无需修改内容的指针传递需使用const声明如:void xx(const int *arry);
  • 返回,不可被外部修改的返回值需使用const声明如:const int *Get_Arry_Addr(void);
  • 类型转换,在不同的类型转换中使用显式的类型转换如:uint16_t ff = 11; int xx = (int)ff;
  • 宏定义,在定义数值时使用带U后缀显式说明这是无符号数,带有运算符的宏必需使用括号
  • 指针的操作,如在*p++语法中需使用括号明确指定语句优先级
  • 对于while(1)语句和for(;;)语句,优先使用for
  • 在基本数据类型中优先使用标准库中<stdint.h>中的数据类型,如:uint8_t int8_t uint16_t uint32_t,在uint64_t在32位和64位中长度不同,它会是平台支持的最大长度类型
  • 在字符串拷贝中优先使用带长度限定的接口,如strncpy显示指定最大拷贝长度
  • 在内存复制中优先使用memmove替代memcpy
  • 在堆内存申请中优先使用calloc替代malloc

防御性编程

在速度要求和代码精简要求不是太高的情况下应注重对参数的判断和临界值的限定。

  • 对入参的判断,防止访问无效内存区域
  • 对数组操作,防止越界访问
  • 声明局部变量定义明确初始化赋值
int exam_fun(uint8_t *str)   
{   
    if(str != NULL)     //  检查“假设指针不为空”这个条件 
    {   
        //正常处理代码               
    } 
    else 
    {  
        //处理错误代码  
    }  
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容