C语言中#的神奇作用
 2016.03.26    |      C/C++    |     AilsonJack    |     暂无评论    |     918 views
By: Ailson Jack
Date: 2016-03-26
个人博客: http://www.only2fire.com/
<p style="text-indent: 2em;">C语言中,在宏里面的’#’和’##’有它非常神奇的作用,下面就来说说具体的用法。</p><p class="artical_littlestyle1">1、一般用法</p><p style="text-indent: 2em;">我们使用#把宏参数变为一个字符串,用##把两个宏参数贴合在一起。<br/></p><p style="text-indent: 2em;">下面的代码是演示代码:</p><pre class="brush:cpp;toolbar:false PrismJs">#include&nbsp;&lt;stdio.h&gt; #define&nbsp;STRING(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#s #define&nbsp;CONNECT(a,b)&nbsp;&nbsp;&nbsp;&nbsp;int(a##e##b) int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;//输出字符串&quot;abcdefg&quot; &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;string:%s\r\n&quot;,STRING(abcdefg)); &nbsp;&nbsp;&nbsp;&nbsp;//2e3输出:2000 &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;connect:%d\r\n&quot;,CONNECT(2,3)); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; }</pre><p style="text-indent: 2em;">运行结果如下图:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.25/1535188783353292.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.25/1535188783353292.png&#39;)"/></p><p class="artical_littlestyle2">2、当宏参数是另一个宏的时候</p><p style="text-indent: 2em;">需要注意的是凡是宏定义里有用&#39;#&#39;或&#39;##&#39;的地方宏参数是不会再展开。<br/><span style="background-color: rgb(118, 146, 60);">1)、</span>非’#’和’##’的情况 <br/><span style="color: rgb(0, 112, 192);">#define TOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; (2)<br/>#define MUL(a,b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (a*b)<br/>printf(“%d*%d=%d\n”, TOW, TOW, MUL(TOW,TOW));</span><br/></p><p style="text-indent: 2em;">这行的宏会被展开为:<br/><span style="color: rgb(0, 112, 192);">printf(“%d*%d=%d\n”, (2), (2), ((2)*(2)));</span><br/></p><p style="text-indent: 2em;">MUL里的参数TOW会被展开为(2)。<br/><span style="background-color: rgb(118, 146, 60);">2)、</span>当有’#’或’##’的时候 <br/><span style="color: rgb(0, 112, 192);">#define A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; (2)<br/>#define STR(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #s<br/>#define CONS(a,b)&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; int(a##e##b)<br/>printf(“int max: %s\n”, STR(INT_MAX)); //INT_MAX 这行会被展开为:printf(“int max: %s\n”, “INT_MAX”);<br/>printf(“%s\n”, CONS(A, A)); //这一行则是:printf(“%s\n”, int(AeA));</span><br/></p><p style="text-indent: 2em;">INT_MAX和A都不会再被展开,然而解决这个问题的方法很简单。加多一层中间转换宏。加这层宏的用意是把所有宏的参数在这层里全部展开,那么在转换宏里的那一个宏(_STR)就能得到正确的宏参数。<br/><span style="color: rgb(0, 112, 192);">#define A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; (2)<br/>#define _STR(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; #s<br/>#define STR(s)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _STR(s) //转换宏<br/>#define _CONS(a,b)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int(a##e##b)<br/>#define CONS(a,b)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _CONS(a,b) //转换宏<br/>printf(“int max: %s\n”, STR(INT_MAX)); // INT_MAX,int型的最大值,为一个变量</span><br/></p><p style="text-indent: 2em;">输出为: <span style="color: rgb(255, 0, 0);">int max: 0x7fffffff</span><span style="color: rgb(0, 0, 0);">,STR(INT_MAX) –&gt; _STR(0x7fffffff)</span>然后再转换成字符串。<br/><span style="color: rgb(0, 112, 192);">printf(“%d\n”, CONS(A, A));</span><br/></p><p style="text-indent: 2em;">输出为:<span style="color: rgb(255, 0, 0);">200</span>,CONS(A, A) –&gt; _CONS((2), (2)) –&gt; int((2)e(2)) 。<br/></p><p class="artical_littlestyle3">3、&#39;#&#39;和&#39;##&#39;的一些应用特例</p><p style="text-indent: 0em;"><span style="background-color: rgb(118, 146, 60);">1)、</span>合并匿名变量名<br/><span style="color: rgb(0, 112, 192);">#define&nbsp;&nbsp; ___ANONYMOUS1(type,var,line)&nbsp;&nbsp;&nbsp; type&nbsp;&nbsp; var##line<br/>#define&nbsp;&nbsp; __ANONYMOUS0(type, line)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ___ANONYMOUS1(type, _anonymous, line)<br/>#define&nbsp;&nbsp; ANONYMOUS(type)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; __ANONYMOUS0(type, __LINE__)</span><br/>例:ANONYMOUS(static int);即: static int _anonymous70;//70表示该行行号;<br/>第一层:<span style="color: rgb(0, 112, 192);">ANONYMOUS(static int);</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(255, 0, 0);">-&gt;</span>&nbsp;&nbsp; <span style="color: rgb(0, 112, 192);">__ANONYMOUS0(static int, __LINE__);</span><br/>第二 层:<span style="color: rgb(0, 112, 192);">__ANONYMOUS0(static int, __LINE__);</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: rgb(255, 0, 0);">-&gt;</span>&nbsp;&nbsp; <span style="color: rgb(0, 112, 192);">___ANONYMOUS1(static int, _anonymous, 70);</span><br/>第三层:<span style="color: rgb(0, 112, 192);">___ANONYMOUS1(static int, _anonymous, 70);</span>&nbsp; <span style="color: rgb(255, 0, 0);">-&gt;</span>&nbsp;&nbsp; <span style="color: rgb(0, 112, 192);">static int&nbsp;&nbsp; _anonymous70;</span><br/>即每次只能解开当前层的宏,所以__LINE__在第二层才能被解开<br/><span style="background-color: rgb(118, 146, 60);">2)、</span>记录文件名<br/><span style="color: rgb(0, 112, 192);">#define&nbsp;&nbsp;&nbsp; _GET_FILE_NAME(f)&nbsp;&nbsp;&nbsp; #f<br/>#define&nbsp;&nbsp;&nbsp; GET_FILE_NAME(f)&nbsp;&nbsp; &nbsp; _GET_FILE_NAME(f)<br/>static char&nbsp; FILE_NAME[] = GET_FILE_NAME(__FILE__); </span><br/></p><p style="text-indent: 2em;">其中2用得比较多,很方便。<br/></p>
欢迎关注博主的公众号呀,精彩内容随时掌握:
热情邀请仔细浏览下博客中的广告,万一有对自己有用或感兴趣的呢。◕ᴗ◕。。
如果这篇文章对你有帮助,记得点赞和关注博主就行了^_^,当然了能够赞赏博主,那就非常感谢啦!
注: 转载请注明出处,谢谢!^_^
转载请注明来源: 本文链接:  By: AilsonJack
C语言中#的神奇作用  |  说好一起走
暂无评论,要不要来个沙发
发表评论

 
Copyright © 2015~2023  说好一起走   保留所有权利   |  百度统计  蜀ICP备15004292号