useful macros

本文介绍了C语言中宏定义的多种实用技巧,包括避免头文件重复包含、类型定义、内存地址操作、数值计算、结构体操作等,并展示了如何利用宏进行调试及避免常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

macro:大陆译为“宏”,台湾译为“巨集”

1,防止一个头文件被重复包含

#ifndef COMDEF_H

#define COMDEF_H

  //头文件内容

#endif

2,重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。

typedef  unsigned char      boolean;     /* Boolean value type. */  

typedef  unsigned long int  uint32;      /* Unsigned 32 bit value */

typedef  unsigned short     uint16;      /* Unsigned 16 bit value */

typedef  unsigned char      uint8;       /* Unsigned 8  bit value */  

typedef  signed long int    int32;       /* Signed 32 bit value */

typedef  signed short       int16;       /* Signed 16 bit value */

typedef  signed char        int8;        /* Signed 8  bit value */  

//下面的不建议使用

typedef  unsigned char     byte;         /* Unsigned 8  bit value type. */

typedef  unsigned short    word;         /* Unsinged 16 bit value type. */

typedef  unsigned long     dword;        /* Unsigned 32 bit value type. */  

typedef  unsigned char     uint1;        /* Unsigned 8  bit value type. */

typedef  unsigned short    uint2;        /* Unsigned 16 bit value type. */

typedef  unsigned long     uint4;        /* Unsigned 32 bit value type. */

 

typedef  signed char       int1;         /* Signed 8  bit value type. */

typedef  signed short      int2;         /* Signed 16 bit value type. */

typedef  long int          int4;         /* Signed 32 bit value type. */

 

typedef  signed long       sint31;       /* Signed 32 bit value */

typedef  signed short      sint15;       /* Signed 16 bit value */

typedef  signed char       sint7;        /* Signed 8  bit value */

 

3,得到指定地址上的一个字节或字

#define  MEM_B( x )  ( *( (byte *) (x) ) )

#define  MEM_W( x )  ( *( (word *) (x) ) )

4,求最大值和最小值 (不是最好的宏定义取最大最小值)

   #define  MAX( x, y ) ( ((x) > (y)) ? (x) : (y) )

   #define  MIN( x, y ) ( ((x) < (y)) ? (x) : (y) )

5,得到一个field在结构体(struct)中的偏移量 (有用)

#define FPOS( type, field ) /

/*lint -e545 */ ( (dword) &(( type *) 0)-> field ) /*lint +e545 */

6,得到一个结构体中field所占用的字节数

#define FSIZ( type, field ) sizeof( ((type *) 0)->field )

7,按照LSB格式把两个字节转化为一个Word

#define  FLIPW( ray ) ( (((word) (ray)[0]) * 256) + (ray)[1] )

8,按照LSB格式把一个Word转化为两个字节

#define  FLOPW( ray, val ) /

  (ray)[0] = ((val) / 256); /

  (ray)[1] = ((val) & 0xFF)

9,得到一个变量的地址(word宽度)

#define  B_PTR( var )  ( (byte *) (void *) &(var) )

#define  W_PTR( var )  ( (word *) (void *) &(var) )

10,得到一个字的高位和低位字节

#define  WORD_LO(***)  ((byte) ((word)(***) & 255))

#define  WORD_HI(***)  ((byte) ((word)(***) >> 8))

11,返回一个比X大的最接近的8的倍数

#define RND8( x )       ((((x) + 7) / 8 ) * 8 )

12,将一个字母转换为大写

#define  UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )

13,判断字符是不是10进值的数字

#define  DECCHK( c ) ((c) >= '0' && (c) <= '9')

14,判断字符是不是16进值的数字

#define  HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||/

                       ((c) >= 'A' && (c) <= 'F') ||/

((c) >= 'a' && (c) <= 'f') )

15,防止溢出的一个方法

#define  INC_SAT( val )  (val = ((val)+1 > (val)) ? (val)+1 : (val))

16,返回数组元素的个数

#define  ARR_SIZE( a )  ( sizeof( (a) ) / sizeof( (a[0]) ) )

17,返回一个无符号数n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)

#define MOD_BY_POWER_OF_TWO( val, mod_by ) /

           ( (dword)(val) & (dword)((mod_by)-1) )

18,对于IO空间映射在存储空间的结构,输入输出处理

  #define inp(port)         (*((volatile byte *) (port)))

  #define inpw(port)        (*((volatile word *) (port)))

  #define inpdw(port)       (*((volatile dword *)(port)))

  

  #define outp(port, val)   (*((volatile byte *) (port)) = ((byte) (val)))

  #define outpw(port, val)  (*((volatile word *) (port)) = ((word) (val)))

  #define outpdw(port, val) (*((volatile dword *) (port)) = ((dword) (val)))

[2005-9-9添加] 

19,使用一些宏跟踪调试

A N S I标准说明了五个预定义的宏名。它们是:

_ L I N E _

_ F I L E _

_ D A T E _

_ T I M E _

_ 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,输出数据信息和所在文件所在行

#ifdef _DEBUG

#define DEBUGMSG(msg,date) printf(msg);printf(“%d%d%d”,date,_LINE_,_FILE_)

#else

      #define DEBUGMSG(msg,date) 

#endif

 

20,宏定义防止使用是错误

用小括号包含。

例如:#define ADD(a,b) (a+b)

用do{}while(0)语句包含多语句防止错误

例如:#difne DO(a,b) a+b;/

                   a++;

应用时:if(….)

          DO(a,b); //产生错误

        else  

关于MFC

其实MFC中的message routing ,command routing内部都是用很妙的宏实现的。

#ifndef nuts_bolts_h #define nuts_bolts_h #define false 0 #define true 1 #define SOME_LARGE_VALUE 1.0E+38 // Axis array index values. Must start with 0 and be continuous. #define N_AXIS 6 // Number of axes #define X_AXIS 0 // Axis indexing value. #define Y_AXIS 1 #define Z_AXIS 2 #define A_AXIS 3 #define B_AXIS 4 #define C_AXIS 5 // CoreXY motor assignments. DO NOT ALTER. // NOTE: If the A and B motor axis bindings are changed, this effects the CoreXY equations. #ifdef COREXY #define A_MOTOR X_AXIS // Must be X_AXIS #define B_MOTOR Y_AXIS // Must be Y_AXIS #endif #define F_CPU SystemCoreClock // Conversions #define MM_PER_INCH (25.40f) #define INCH_PER_MM (0.0393701f) #define TICKS_PER_MICROSECOND (F_CPU/1000000) #define DELAY_MODE_DWELL 0 #define DELAY_MODE_SYS_SUSPEND 1 // Useful macros #define clear_vector(a) memset(a, 0, sizeof(a)) #define clear_vector_float(a) memset(a, 0.0, sizeof(float)*N_AXIS) // #define clear_vector_long(a) memset(a, 0.0, sizeof(long)*N_AXIS) #define max(a,b) (((a) > (b)) ? (a) : (b)) #define min(a,b) (((a) < (b)) ? (a) : (b)) #define isequal_position_vector(a,b) !(memcmp(a, b, sizeof(float)*N_AXIS)) // Bit field and masking macros #define bit(n) (1 << n) #define bit_true(x,mask) (x) |= (mask) #define bit_false(x,mask) (x) &= ~(mask) #define bit_istrue(x,mask) ((x & mask) != 0) #define bit_isfalse(x,mask) ((x & mask) == 0) // Read a floating point value from a string. Line points to the input buffer, char_counter // is the indexer pointing to the current character of the line, while float_ptr is // a pointer to the result variable. Returns true when it succeeds uint8_t read_float(char *line, uint8_t *char_counter, float *float_ptr); // Non-blocking delay function used for general operation and suspend features. void delay_sec(float seconds, uint8_t mode); // Delays variable-defined milliseconds. Compiler compatibility fix for _delay_ms(). void delay_ms(uint16_t ms); // Delays variable-defined microseconds. Compiler compatibility fix for _delay_us(). void delay_us(uint32_t us); // Computes hypotenuse, avoiding avr-gcc's bloated version and the extra error checking. float hypot_f(float x, float y); float convert_delta_vector_to_unit_vector(float *vector); float limit_value_by_axis_maximum(float *max_value, float *unit_vec); #endif
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值