Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
预处理命令
|
261
预处理命令
15.2.2.3 记号粘贴运算符
##
运算符是一个二元运算符,可以出现在所有宏的替换文本中。该运算符会把左、右操
作数结合在一起,作为一个记号,因此,它常常被称为记号粘贴运算符(
token-pasting
operator
)。如果结果文本中还包含有宏名称,则预处理器会继续进行宏替换。出现在
##
运算
符前后的空白符连同
##
运算符本身一起被删除。
通常,使用
##
运算符时,至少有一个操作数是宏的形参。在这种情况下,实参值会先替
换形参,然后等记号粘贴完成后,才进行宏展开。如下例所示:
#define TEXT_A "Hello, world!"
#define msg(x) puts( TEXT_ ## x )
msg(A);
无论标识符
A
是否定义为一个宏名称,预处理器会先将形参
x
替换成实参
A
,然后进行记
号粘贴。当这两个步骤做完后,结果如下:
puts( TEXT_A );
现在,因为
TEXT_A
是一个宏名称,后续的宏替换会生成下面的语句:
puts( "Hello, world!" );
如果宏的形参是
##
运算符的操作数,并且在某次宏调用时,并没有为该形参准备对应的
实参,那么预处理使用占位符(
placeholder
)表示该形参被空字符串取代。把一个占位
符和任何记号进行记号粘贴操作的结果还是原来的记号。如果对两个占位符进行记号粘
贴操作,则得到一个占位符。当所有的记号粘贴运算都做完后,预处理器会删除所有剩
下的占位符。下面是一个示例,调用宏时传入空的实参:
msg();
这个调用会被展开为如下所示的代码: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207