写好C语言,漂亮的宏定义很重要!
共 3631字,需浏览 8分钟
·
2022-03-01 22:51
来源:玩转嵌入式
写好C语言,漂亮的宏定义很重要!使用宏定义可以防止出错,提高可移植性,可读性,方便性等。下面列举了一些成熟软件中常用的宏定义。
来源:玩转嵌入式
1. 防止一个头文件被重复包含
1#ifndef COMDEF_H
2#define COMDEF_H
3//头文件内容
4#endif
1#ifndef COMDEF_H
2#define COMDEF_H
3//头文件内容
4#endif
2. 重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。
1typedef unsigned char boolean; /* Boolean value type. */
2typedef unsigned long int uint32; /* Unsigned 32 bit value */
3typedef unsigned short uint16; /* Unsigned 16 bit value */
4typedef unsigned char uint8; /* Unsigned 8 bit value */
5typedef signed long int int32; /* Signed 32 bit value */
6typedef signed short int16; /* Signed 16 bit value */
7typedef signed char int8; /* Signed 8 bit value */
不建议使用:
1typedef unsigned char byte; /* Unsigned 8 bit value type. */
2typedef unsigned short word; /* Unsinged 16 bit value type. */
3typedef unsigned long dword; /* Unsigned 32 bit value type. */
4typedef unsigned char uint1; /* Unsigned 8 bit value type. */
5typedef unsigned short uint2; /* Unsigned 16 bit value type. */
6typedef unsigned long uint4; /* Unsigned 32 bit value type. */
7typedef signed char int1; /* Signed 8 bit value type. */
8typedef signed short int2; /* Signed 16 bit value type. */
9typedef long int int4; /* Signed 32 bit value type. */
10typedef signed long sint31; /* Signed 32 bit value */
11typedef signed short sint15; /* Signed 16 bit value */
12typedef signed char sint7; /* Signed 8 bit value */
1typedef unsigned char boolean; /* Boolean value type. */
2typedef unsigned long int uint32; /* Unsigned 32 bit value */
3typedef unsigned short uint16; /* Unsigned 16 bit value */
4typedef unsigned char uint8; /* Unsigned 8 bit value */
5typedef signed long int int32; /* Signed 32 bit value */
6typedef signed short int16; /* Signed 16 bit value */
7typedef signed char int8; /* Signed 8 bit value */
不建议使用:
1typedef unsigned char byte; /* Unsigned 8 bit value type. */
2typedef unsigned short word; /* Unsinged 16 bit value type. */
3typedef unsigned long dword; /* Unsigned 32 bit value type. */
4typedef unsigned char uint1; /* Unsigned 8 bit value type. */
5typedef unsigned short uint2; /* Unsigned 16 bit value type. */
6typedef unsigned long uint4; /* Unsigned 32 bit value type. */
7typedef signed char int1; /* Signed 8 bit value type. */
8typedef signed short int2; /* Signed 16 bit value type. */
9typedef long int int4; /* Signed 32 bit value type. */
10typedef signed long sint31; /* Signed 32 bit value */
11typedef signed short sint15; /* Signed 16 bit value */
12typedef signed char sint7; /* Signed 8 bit value */
3. 得到指定地址上的一个字节或字
1#define MEM_B( x ) ( *( (byte *) (x) ) )
2#define MEM_W( x ) ( *( (word *) (x) ) )
1#define MEM_B( x ) ( *( (byte *) (x) ) )
2#define MEM_W( x ) ( *( (word *) (x) ) )
4. 求最大值和最小值
1#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
2#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
1#define MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )
2#define MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )
5. 得到一个field在结构体(struct)中的偏移量
1#define FPOS( type, field ) \
2/*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */
1#define FPOS( type, field ) \
2/*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */
6. 得到一个结构体中field所占用的字节数
1#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
1#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
7. 按照LSB格式把两个字节转化为一个Word
1#define FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )
1#define FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )
8. 按照LSB格式把一个Word转化为两个字节
1#define FLOPW( ray, val ) \
2(ray)[0] = ((val) / 256); \
3(ray)[1] = ((val) & 0xFF)
1#define FLOPW( ray, val ) \
2(ray)[0] = ((val) / 256); \
3(ray)[1] = ((val) & 0xFF)
9. 得到一个变量的地址(word宽度)
1#define B_PTR( var ) ( (byte *) (void *) &(var) )
2#define W_PTR( var ) ( (word *) (void *) &(var) )
1#define B_PTR( var ) ( (byte *) (void *) &(var) )
2#define W_PTR( var ) ( (word *) (void *) &(var) )
10. 得到一个字的高位和低位字节
1#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
2#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
1#define WORD_LO(xxx) ((byte) ((word)(xxx) & 255))
2#define WORD_HI(xxx) ((byte) ((word)(xxx) >> 8))
11. 返回一个比X大的最接近的8的倍数
1#define RND8( x ) ((((x) + 7) / 8 ) * 8 )
1#define RND8( x ) ((((x) + 7) / 8 ) * 8 )
12. 将一个字母转换为大写
1#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
1#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
13. 判断字符是不是10进制的数字
1#define DECCHK( c ) ((c) >= '0' && (c) <= '9')
1#define DECCHK( c ) ((c) >= '0' && (c) <= '9')
14. 判断字符是不是16进制的数字
1#define HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||\
2((c) >= 'A' && (c) <= 'F') ||\
3((c) >= 'a' && (c) <= 'f') )
1#define HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||\
2((c) >= 'A' && (c) <= 'F') ||\
3((c) >= 'a' && (c) <= 'f') )
15. 防止溢出的一个方法
1#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
1#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
16. 返回数组元素的个数
1#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
1#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )
17. 返回一个无符号数n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)
1#define MOD_BY_POWER_OF_TWO( val, mod_by ) \
2( (dword)(val) & (dword)((mod_by)-1) )
1#define MOD_BY_POWER_OF_TWO( val, mod_by ) \
2( (dword)(val) & (dword)((mod_by)-1) )
18. 对于IO空间映射在存储空间的结构,输入输出处理:
1#define inp(port) (*((volatile byte *) (port)))
2#define inpw(port) (*((volatile word *) (port)))
3#define inpdw(port) (*((volatile dword *)(port)))
4#define outp(port, val) (*((volatile byte *) (port)) = ((byte) (val)))
5#define outpw(port, val) (*((volatile word *) (port)) = ((word) (val)))
6#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))
1#define inp(port) (*((volatile byte *) (port)))
2#define inpw(port) (*((volatile word *) (port)))
3#define inpdw(port) (*((volatile dword *)(port)))
4#define outp(port, val) (*((volatile byte *) (port)) = ((byte) (val)))
5#define outpw(port, val) (*((volatile word *) (port)) = ((word) (val)))
6#define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))
19. 使用一些宏跟踪调试
A N S I标准说明了五个预定义的宏名,其分别是:
1_ L I N E _
2_ F I L E _
3_ D A T E _
4_ T I M E _
5_ S T D C _
如果编译不是标准的,则可能仅支持以上宏名中的几个,或根本不支持。记住编译程序也许还提供其它预定义的宏名。 _ L I N E _
及_ F I L E _
宏指令在有关# l i n e
的部分中已讨论,这里讨论其余的宏名。_ D AT E _
宏指令含有形式为月/日/年的串,表示源文件被翻译到代码时的日期。源代码翻译到目标代码的时间作为串包含在_ T I M E _
中。串形式为时:分:秒。 如果实现是标准的,则宏_ S T D C _
含有十进制常量1。如果它含有任何其它数,则实现是非标准的。 可以定义宏,例如:当定义了_DEBUG
,输出数据信息和所在文件所在行。 1#ifdef _DEBUG
2#define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
3#else
4#define DEBUGMSG(msg,date)
5#endif
A N S I标准说明了五个预定义的宏名,其分别是:
1_ L I N E _
2_ F I L E _
3_ D A T E _
4_ T I M E _
5_ S T D C _
_ L I N E _
及_ F I L E _
宏指令在有关# l i n e
的部分中已讨论,这里讨论其余的宏名。_ D AT E _
宏指令含有形式为月/日/年的串,表示源文件被翻译到代码时的日期。_ T I M E _
中。串形式为时:分:秒。_ S T D C _
含有十进制常量1。如果它含有任何其它数,则实现是非标准的。_DEBUG
,输出数据信息和所在文件所在行。1#ifdef _DEBUG
2#define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)
3#else
4#define DEBUGMSG(msg,date)
5#endif
20. 宏定义防止使用时错误用小括号包含。
例如:
1#define ADD(a,b) (a+b)
用do{}while(0)
语句包含多语句防止错误,例如:
1#difne DO(a,b) a+b;\
2a++;
应用时:
1if(….)
2DO(a,b); //产生错误
3else
解决方法:
1#define DO(a,b) do{a+b;\
2a++;}while(0)
例如:
1#define ADD(a,b) (a+b)
用do{}while(0)
语句包含多语句防止错误,例如:
1#difne DO(a,b) a+b;\
2a++;
应用时:
1if(….)
2DO(a,b); //产生错误
3else
解决方法:
1#define DO(a,b) do{a+b;\
2a++;}while(0)