#define macros Advanced Usage

Original: https: //blog.csdn.net/xiaoxu2050/article/details/82893476

First, the macro definition and revocation

#普通宏定义
#define PI 3.14    //编译阶段替换掉宏
#define T1 3+4     //容易产生歧义
#define T2 (3+4)   //添加括号后,语义清楚
 
float r = 1.0;
float area = PI * r * r;    
int a = 2* T1    #宏替换后变成   int a = 2*3+4     不符合本意    
ing a = 2* T2    #红替换后变成   int a = 2*(3+4)   符合本意   
 
#undef PI
float area = PI * r * r;     #error: ‘PI’ was not declared in this scope
 
//引号中的宏定义不会被替换
printf("%s:%f\n", "PI", PI);    //输出 PI:3.14
 
//宏定义的名字必须是合法标识符
#define 0x abcd    //error 不能以数字开始
 
//宏定义中双引号和单引号必须成对出现
#define TEST11 "Z    //error
#define TEST2 'Z     //error

Second, the macro definition with parameters

//max和min的宏定义带参数
#define MAX(a,b) (a>b ? a:b)
#define MIN(a,b) (a<b ? a:b)
 
//使用带参数的宏定义
int sum= MAX(1,2) + MIN(1,2);    //替换后语句为:int sum = (1>2 ? 1:2) + (1<2 ? 1:2)
 
//参数个数必须宏定义时形参的个数相同
MAX(1,2,3);    //会报错
 
#undef MAX    //撤销MAX的宏定义
MAX(1,2);    //error: ‘MAX’ was not declared in this scope

Third, multi-line macros using backticks \ connection

#定义一个交换数值的多行宏,使用反斜杠连接不同行
#define SWAP(a,b) do { \
    int t = 0;\
    t = a; \
    a = b; \
    b = t; \
} while(0)

Four, three special symbols: # ##, # @

#define CONNECT(a,b) a##b
#define TOCHAR(a) #@a
#define TOSTRING(a) #a
 
//a##b表示连接
int n = CONNECT(123, 456);                //结果  n = 123456
char *str = CONNECT("abcd", "efg");       //结果  str = "abcdefg"
 
//@#a 表示用单引号包括参数a,返回的是一个字符
char * ch1 = TOCHAR(1);        //结果  ch = '1'
char * ch2 = TOCHAR(123);      //报错,单引号只用在单个字符里
 
//#a 表示用双引号包括参数a,返回一个字符串
char * str1 = TOSTRING(123);    // str = "123"

Fifth, the common macro definitions

  • Header files being included to prevent the
#ifndef BODYDEF_H 
#define BODYDEF_H 
 
//头文件内容 
 
#endif
  • To give a byte or word value on the specified address
#include "stdio.h"
//B表示字节byte
#define MEM_B( x )  ( *( (byte *) (x) ) )
//B表示字word,可以理解为int
#define MEM_W( x )  ( *( (word *) (x) ) )
 
 
int main()
{
    int bTest = 0x123456;
 
    byte m = MEM_B((&bTest));    /*m=0x56*/
    int n = MEM_W((&bTest));     /*n=0x3456*/
    
    return 0;
}
  • To obtain a field offset structure (struct) in
#define OFFSETOF( type, field ) ( (size_t) &(( type *) 0)-> field )
  • A structure having a number of bytes occupied by the field
#define FSIZ( type, field ) sizeof( ((type *) 0)->field )
  • To obtain the address of a variable (word width)
#define B_PTR( var ) ( (byte *) (void *) &(var) ) 
#define W_PTR( var ) ( (word *) (void *) &(var) )
  • Convert a letter to uppercase
#define UPCASE( c ) ( ((c) >= 'a' && (c) <= 'z') ? ((c) - 0x20) : (c) )
  • Analyzing the character is not numeric values ​​into 10
#define DECCHK( c ) ((c) >= '0' && (c) <= '9')
  • Analyzing the character is not numeric values ​​into 16
#define HEXCHK( c ) ( ((c) >= '0' && (c) <= '9') ||((c) >= 'A' && (c) <= 'F') ||((c) >= 'a' && (c) <= 'f') )
  • A method for preventing overflow
#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))
  • Returns the number of array elements
#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )

1, learn more embedded dry goods please pay attention to micro-channel public number [Hundred Questions Science and Technology]
2, technical discussions please add personal micro letter: 13,266,630,429
3. Wei Dongshan is facing old student recruitment agents and distributors, commission of 20%, are interested please contact individuals micro letter.

Published 135 original articles · won praise 401 · views 260 000 +

Guess you like

Origin blog.csdn.net/thisway_diy/article/details/102701325