C语言_Coding Style
发布日期:2021-05-07 14:37:18 浏览次数:28 分类:原创文章

本文共 5841 字,大约阅读时间需要 19 分钟。

@/"...

1> 文件结构

1.1> 头文件结构

/**  ******************************************************************************  * @file           : demo.h  * @author         : X  * @date           : 04/11, 2021  * @brief          : Just Do It  *                    ******************************************************************************  * @attention  *	  *  ******************************************************************************  */#ifndef __DEMO_H__#define __DEMO_H__#ifdef __cplusplusextern "C" {   #endif/* 0> Includes ---------------------------------------------------------------*//* 1> Exported Types ---------------------------------------------------------*//* 2> Exported Constants -----------------------------------------------------*//* 3> Exported macro ---------------------------------------------------------*//* 4> Exported functions -----------------------------------------------------*//* 5> Private types ----------------------------------------------------------*//* 6> Private Constants ------------------------------------------------------*//* 7> Private macros ---------------------------------------------------------*//* 8> Private functions ------------------------------------------------------*/#ifdef __cplusplus}#endif#endif /* End of file ****************************************************************/ 

1.2> C文件结构

/**---------------------Copyright (C) SUB Corporation-------------------------** * @File     mani.c * @Author   X@/'' * @Version  V1.0 * @Date     12/13, 2020 * **---------------------------------------------------------------------------** * @Description *  *  * **---------------------------------------------------------------------------** * @Attention * **---------------------------------------------------------------------------*//* @Includes -----------------------------------------------------------------*/模块1> "头文件包含"/* @Private typedef ----------------------------------------------------------*/模块2> "数据类型声明"/* @Private constants --------------------------------------------------------*/模块3> "常量定义"/* @Private macro ------------------------------------------------------------*/模块4> "宏函数"/* @Private variables --------------------------------------------------------*/模块5> "变量声明"/* @Private function prototypes ----------------------------------------------*/模块6> "私有函数声明"/* @Private functions --------------------------------------------------------*/模块7> "宏函实现"/** * @brief * @param * @retval * */int main (){   		while (1) {   		;	}}/* main.c */

2> 代码示例

2.1> 函数示例

int hex_to_bin(char ch){   	if ((ch >= '0') && (ch <= '9'))		return ch - '0';	ch = tolower(ch);	if ((ch >= 'a') && (ch <= 'f'))		return ch - 'a' + 10;	return -1;}-----------------------------------------

2.2> if 语句

if (FLOAT(theta) > 90) {   	theta -= FIXED(180);	signx = -1;} else if (FLOAT(theta) < -90) {   	theta += FIXED(180);	signx = -1;}

2.3> for语句

for (i = 0; i < nSelectors; i++) {   }

2.4> do-while语句

int strcasecmp(const char *s1, const char *s2){   	int c1, c2;	do {   		c1 = tolower(*s1++);		c2 = tolower(*s2++);	} while (c1 == c2 && c1 != 0);	return c1 - c2;}

2.5> Switch语句

int strtobool(const char *s, bool *res){   	switch (s[0]) {   	case 'y':	case 'Y':	case '1':		*res = true;		break;	case 'n':	case 'N':	case '0':		*res = false;		break;	default:		return -EINVAL;	}	return 0;}

2.6> Typedef

/* 结构体中不使用 */struct rt_mutex_waiter {   	struct plist_node	list_entry;	struct plist_node	pi_list_entry;	struct task_struct	*task;	struct rt_mutex		*lock;};struct rt_mutex_waiter *w;

总结:

<1> 函数

名称: 全小写,采用下划线风格;
功能: 一次只做一件事,短小精悍
局部变量: 不超过5~10个;

<2> 变量

局部变量:全小写,简短;
全局变量:g_xx
静态变量:s_xx

3> 注释

3.1> 函数注释

/** * textsearch_find_continuous - search a pattern in continuous/linear data * @conf: search configuration * @state: search state * @data: data to search in * @len: length of data * * A simplified version of textsearch_find() for continuous/linear data. * Call textsearch_next() to retrieve subsequent matches. * * Returns the position of first occurrence of the pattern or * %UINT_MAX if no occurrence was found. */ unsigned int textsearch_find_continuous(struct ts_config *conf,					struct ts_state *state,					const void *data, unsigned int len){   	struct ts_linear_state *st = (struct ts_linear_state *) state->cb;	conf->get_next_block = get_linear_data;	st->data = data;	st->len = len;	return textsearch_find(conf, state);}

3.2> 函数内部注释

int stmp_reset_block(void __iomem *reset_addr){   	int ret;	int timeout = 0x400;	/* clear and poll SFTRST */	ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);	if (unlikely(ret))		goto error;	/* clear CLKGATE */	writel(STMP_MODULE_CLKGATE, reset_addr + STMP_OFFSET_REG_CLR);	/* set SFTRST to reset the block */	writel(STMP_MODULE_SFTRST, reset_addr + STMP_OFFSET_REG_SET);	udelay(1);	/* poll CLKGATE becoming set */	while ((!(readl(reset_addr) & STMP_MODULE_CLKGATE)) && --timeout)		/* nothing */;	if (unlikely(!timeout))		goto error;	/* clear and poll SFTRST */	ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_SFTRST);	if (unlikely(ret))		goto error;	/* clear and poll CLKGATE */	ret = stmp_clear_poll_bit(reset_addr, STMP_MODULE_CLKGATE);	if (unlikely(ret))		goto error;	return 0;error:	pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);	return -ETIMEDOUT;}

总结:

不过度注重

4> 常用缩写

arg		argumnet-----------------------buf		buffer-----------------------clk		clock			cmd		command			cmp		compare			cfg		configuration	-----------------------dev		devicedisp	display-----------------------err		error-----------------------hex		hexadecimal-----------------------inc		incrementinit	initialize-----------------------max		maximummsg		messagemin		minimum-----------------------param	parameterprev	previousreg		register-----------------------sem		semaphorestat	statisticsync	synchronizetmp		temp-----------------------

5> 参考

<1> ST标准库;
<2> Linux 内核源码;

上一篇:F407_06_标准库开发
下一篇:F407_03_启动

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月07日 08时55分46秒