#define用法

宏定义

作用范围:定义行后的语句块,可以是全局宏或局部宏

本质:宏替换(在程序中使用宏名,在预处理(或预编译)时期进行内容替换)

好处:

1、提高可读性

2、对一些简单的函数进行宏定义,无需函数调用,运行效率高。

3、 可维护性好

 

 

define中的三个特殊符号

1、##。

宏定义:#define Conn(x,y) x##y

x,y的连接,可以返回int/浮点类型的数据(不能连接两个浮点数),也可以连接变量,等等

 1 ...
 2 
 3 #define ADD_TO_ax(x,val) a##x += val
 4 
 5 int main()
 6 {
 7     int a1 = 0;
 8     ADD_TO_ax(1, 5);
 9     cout<<a1; 
10     return 0;
11 }
2\

2、#@

宏定义:#define ToChar(x) #@x

返回const char类型字符,x只能是单个字符,否则编译器报错或返回最后一个字符

3、#

宏定义:#define ToString(x) #x

返回const char *类型,即“x”

注意:三种宏定义括号中的变量名全为常量,不能表示变量

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 #define Conn(x,y) x##y
 5 #define ToChar(x) #@x
 6 #define ToString(x) #x
 7 
 8 int main()
 9 {
10     float a = Conn(1.1, 2);
11     char ch = ToChar(e);
12     ch = 'a';
13     char *s = ToString(ch);//括号中部分默认为常量
14     cout << a << endl << ch << endl << s << endl;
15     return 0;
16 }
View Code

 

 

常见的宏定义:

1、防止头文件被重复包含

1 #ifndef BODYDEF_H 
2 #define BODYDEF_H 
3 
4  //头文件内容 
5  
6 #endif
View Code

 

2、得到指定地址的一个字节或字

宏定义:

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

 1 ...
 2 
 3 #include <Windows.h>
 4 
 5 #define MEM_B(x) (*((byte*)(x)))
 6 #define MEM_W(x) (*((WORD*)(x)))
 7 
 8 int main()
 9 {
10     int test = 0x11221221;
11     byte m = MEM_B(&test);//得到一个字节内容,0x21
12     int n = MEM_W(&test);//得到一个字的内容,0x1221
13     printf("0x%x
", m);
14     printf("0x%x
", n);
15     printf("%d
", m);//2*16+1=33
16     return 0;
17 }
View Code

 

 3、字母字符大小写转换

#define UPCASE(ch) ( ( ch<='Z' && ch>='A' )?(ch - 0x20):ch ) //将字符变量ch转为大写
#define LOWCASE(ch) ( ( ch<='Z' && ch>='A' )?(ch + 0x20):ch ) //将字符变量ch转为小写

 1 ...
 2 
 3 #define UPCASE(ch) ( ( ch<='z' && ch>='a' )?(ch - 0x20):ch ) //将字符变量ch转为大写
 4 #define LOWCASE(ch) ( ( ch<='Z' && ch>='A' )?(ch + 0x20):ch ) //将字符变量ch转为小写
 5 int main()
 6 {
 7     char *s = "Hi,Adam",*p;
 8     char str[10];
 9     for (p = s; *p != ''; p++)
10         str[p - s] = UPCASE(*p);
11     str[p - s] = '';
12     cout << str << endl;
13     cout << "-----------------" << endl;
14     cout << UPCASE('a') << endl;
15     cout << "-----------------" << endl;
16     char ch = 'a';
17     ch = UPCASE(ch);
18     cout << ch << endl;
19     return 0;
20 }
21 
22 ...
View Code

 

4、判断字符是不是n进制的

#define DECCHK_CHAR(ch) ( ch<='9' && ch>='0' ) //判断字符是否是10进制
#define OCTCHK_CHAR(ch) ( ch<='7' && ch>='0' ) //判断字符是否是8进制
#define HEXCHK_CHAR(ch) ( (ch<='9'&&ch>='0')||(ch<='f'&&ch>='a')||(ch<="F"&&ch>='A') ) //判断字符是否为16进制

 1 ...
 2 
 3 //判断n进制
 4 #define DECCHK_CHAR(ch) ( ch<='9' && ch>='0' )
 5 #define OCTCHK_CHAR(ch) ( ch<='7' && ch>='0' ) 
 6 #define HEXCHK_CHAR(ch) ( (ch<='9'&&ch>='0')||(ch<='f'&&ch>='a')||(ch<="F"&&ch>='A') )
 7 
 8 int main()
 9 {
10     char ch = '2';
11     cout << DECCHK_CHAR(ch) << endl;
12     cout << DECCHK_CHAR(3) << endl;//错误写法,"形参"是char类型方可
13     return 0;
14 }
View Code

 

5、得到数组的大小

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

 

 1 ...
 2 
 3 #define ARR_SIZE(a) ( sizeof( a )/sizeof( a[0] ))
 4 
 5 int main()
 6 {
 7     char a[3];
 8     cout << ARR_SIZE(a);
 9     return 0;
10 }
View Code

 6、空间申请

  #define MALLOC(num,type) (type*)malloc(num*sizeof(type))

 7、程序中间输出,观察程序某一位置数据情况

其他用法:

#define DO_FOREVER for(;;)  //定义一个死循环

#define REG register  //定义REG来作为register的别名

#define CASE break;case  //在switch中用CASE来补上break;

#define DEBUG_PRINT printf("file:%s line:%d date:%s time:%s ",

__FILE__, __LINE__, __DATE__, __TIME__);  //测试预定义符号

原文地址:https://www.cnblogs.com/guoyujiang/p/11837605.html