C语言中宏的技巧

#include
#include

#define STR(s) #s
#define CONS(a,b) (int)(a##e##b)

int main(void)
{
printf(“int max %s\n”,STR(INT_MAX));
printf(“%d\n”,CONS(2,3));
return 0;
}

使用单个#号的时候,参数会变作为字符串被替换。
使用两个##号时,参数会连接在一起。

BTW:网上能找到的一篇被大量转载的文章(C语言宏定义技巧)中的代码有问题,上面的代码可以完全正常运行。

This entry was posted in C, 编程相关. Bookmark the permalink.

One Response to C语言中宏的技巧

  1. luguo says:

    ##不宜过分使用,你昨天看的代码可以说就属于滥用了~!

    另外,建议你看看我写的这篇论文:
    http://wangcong.org/down/lkllp-wangcong.pdf
    里面有很多C语言方面的技巧~!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>