文件名后缀使用小写
文件名/函数名/变量名采用小驼峰式命名法(lower camel case)
宏定义采用全大写加下划线方式
常量使用宏定义,常见的0/1可例外
变量/类型不同名
public头文件放入public目录,引用时使用 #include <>
private头文件与源文件放入同一目录,引用时使用 #include ""
在头文件声明public函数
不声明private函数,使用static类型实现于调用者之前
static变量/函数不放入头文件
不使用全局变量,改为set和get操作的static变量
/* test.h */
#include <vxWorks.h>
extern int myDataGet();
extern void myDataSet(int data);
extern void myTest();
/* test.c */
#include <stdio.h>
#include <test.h>
static int myData = 0;
int myDataGet()
{ // 行首应有4空格缩进,但当前编辑器不支持
return myData;
}
void myDataSet(int data)
{
myData = data;
}
static int myDataUpdate()
{
return (myData+1);
}
void myTest()
{
myData = myDataUpdate();
printf("myData is %d\n", myData);
return;
}
入参和返回值不直接使用结构体,改用指针
/* test.h */
#include <vxWorks.h>
#define TYPE_INT32 0
#define TYPE_INT16 1
typedef struct
{
int data1;
short data2;
}MY_STRUCT1;
typedef union
{
int data1;
short data2;
}MY_UNION1;
typedef struct
{
int type;
MY_UNION1 data;
}MY_STRUCT2;
extern MY_STRUCT1 *myTest(MY_UNION1 *data);
/* test.c */
#include <stdlib.h>
#include "test.h"
MY_STRUCT1 *myTest(MY_UNION1 *data)
{
MY_STRUCT1 *pStr = (MY_STRUCT1 *)malloc(sizeof(MY_STRUCT1));
...
return pStr;
}
函数指针需有完整声明
数据类型不同时,需使用显式类型转换,例如前文的malloc()
局部变量在其作用域起始位置声明
void test(int num)
{
int data = 0;
int i;
for(i=0; i<num; i++)
{
int j;
for(j=0; j<num; j++)
{
data += i*NUM_MAX + j;
}
}
printf("data: %d\n", data);
}