当前位置:学者斋 >

计算机 >C语言 >

C语言的宏定义分析

C语言的宏定义分析

引导语:你了解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("%sn", QUOTEME(1+2));

替换后==>

printf("%sn", "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: %dn", _replace_1( Older, Small ) );

// The parameters to _replace_2 are substituted before the call

// to _replace_1, so we get NewerLarge.

printf("Check 2: %dn", _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=%sn", QUOTEME(FOO));

扩展后==>

printf("FOO=%sn", "bar");

标签: 语言
  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/cyuyan/pxpn9.html