Mac OSX/iPhone 上的 C 预处理器,使用 '# 钥匙?
我正在查看一些开源项目,我看到以下内容:
NSLog(@"%s w=%f, h=%f", #size, size.width, size.height)
大小符号之前的“#”到底是什么意思? 这是 C 字符串的某种前缀吗?
I'm looking at some open source projects and I'm seeing the following:
NSLog(@"%s w=%f, h=%f", #size, size.width, size.height)
What exactly is the meaning of '#' right before the size symbol? Is that some kind of prefix for C strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了详细说明 dirkgently 的答案,这看起来像是一个宏的实现,它接受 NSSize (或类似的)参数,并打印变量的名称(这就是 # 所做的;将变量的名称转换为包含的字符串)变量的名称),然后是它的值。 因此,在:中,
宏将扩展为:
并打印:(
类似于 NSStringFromSize,但带有变量名称)
To elaborate on dirkgently's answer, this looks like the implementation of a macro that takes an NSSize (or similar) argument, and prints the name of the variable (which is what the # is doing; converting the name of the variable to a string containing the name of the variable) and then its values. So in:
the macro would expand to:
and print:
(similar to NSStringFromSize, but with the variable name)
#
的正式名称是字符串化运算符。 它采用其参数并将其用引号括起来以形成 C 字符串常量,并根据需要转义任何嵌入的引号或反斜杠。 它只允许在宏定义中使用——在常规代码中不允许使用。 例如:相关的预处理器运算符是标记粘贴运算符
##
。 它需要两个令牌并将它们粘贴在一起以获得一个令牌。 与字符串化运算符一样,它只允许在宏定义中使用,而不能在常规代码中使用。The official name of
#
is the stringizing operator. It takes its argument and surrounds it in quotes to make a C string constant, escaping any embedded quotes or backslashes as necessary. It is only allowed inside the definition of a macro -- it is not allowed in regular code. For example:A related preprocessor operator is the token-pasting operator
##
. It takes two tokens and pastes them together to get one token. Like the stringizing operator, it is only allowed in macro definitions, not in regular code.这是宏定义的主体吗? 然后
#
可用于字符串化以下标识符,即打印“字符串”(不带代码)。Is this the body of a macro definition? Then the
#
could be used to stringize the following identifier i.e. to print "string" (without the codes).