- 相关推荐
C语言的宏定义分析
引导语:你了解C语言吗,知道C语言的宏定义是什么吗,以下是百分网小编分享给大家的C语言的宏定义分析,欢迎阅读!
C语言中,预处理器功能:
1. #include <>or" " 的头文件替换
2.#define 对象替换(object-like)
对象替换以第一个空格为分割,之后的为replacement token list
3.#define () 函数替换(function-like)
函数替换 ()之间不能有任何空白符。但是调用的时候可以在之间有空格。
函数替换的时候需要注意参数表的优先级和类型。如果替换块中需要用--';'是,用do{}while(0)封装,
另外注意宏定义末尾不能有';'否则if-else语句的时候容易出错。
4 #ifdefine等条件编译选项
宏定义中比较容易搞错的是##与#的使用。
##是连接两个参数,
#define MYCASE(item,id) \
case id: \
item##_##id = id;\
break
switch(x) {
MYCASE(widget,23);
}
MYCASE(widget,23); 被扩展为
case 23:
widget_23 = 23;
break;
#是把参数变为字符串
#define QUOTEME(x) #x
printf("%s\n", QUOTEME(1+2));
替换后==>
printf("%s\n", "1+2");
在使用##与#的时候,如果想使宏一定的参数也被宏替换(使用其值)
而不是参数名字被使用,应该使用间接访问的方式。
下面是两个例子:
-----------------------------------------------------------------------------------------------------------
enum {
OlderSmall = 0,
NewerLarge = 1
};
#define Older Newer
#define Small Large
#define _replace_1(Older, Small) Older##Small
#define _replace_2(Older, Small) _replace_1(Older, Small)
void printout( void )
{
// _replace_1( Older, Small ) becomes OlderSmall (not NewerLarge),
// despite the #define calls above.
printf("Check 1: %d\n", _replace_1( Older, Small ) );
// The parameters to _replace_2 are substituted before the call
// to _replace_1, so we get NewerLarge.
printf("Check 2: %d\n", _replace_2( Older, Small ) );
}
results is:
Check 1: 0
Check 2: 1
-----------------------------------------------------------------------------
#define FOO bar
#define QUOTEME_(x) #x
#define QUOTEME(x) QUOTEME_(x)
the code
printf("FOO=%s\n", QUOTEME(FOO));
扩展后==>
printf("FOO=%s\n", "bar");
【C语言的宏定义分析】相关文章:
怎样学习c++c语言编程04-28
C语言入门知识07-20
C语言的编码规范02-10
C语言的基本构成12-05
c语言的优点介绍04-13
C语言试题训练10-20
C语言跳出循环10-16
C语言程序的实现09-27
什么是C语言数组04-15
C语言考点习题09-28