Mac OSX/iPhone 上的 C 预处理器,使用 '# 钥匙?

发布于 2024-07-18 02:43:55 字数 171 浏览 10 评论 0原文

我正在查看一些开源项目,我看到以下内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追星践月 2024-07-25 02:43:55

为了详细说明 dirkgently 的答案,这看起来像是一个宏的实现,它接受 NSSize (或类似的)参数,并打印变量的名称(这就是 # 所做的;将变量的名称转换为包含的字符串)变量的名称),然后是它的值。 因此,在:中,

NSSize fooSize = NSMakeSize(2, 3);
MACRO_NAME_HERE(fooSize);

宏将扩展为:

NSLog(@"%s w=%f h=%f", "fooSize", fooSize.width, fooSize.height);

并打印:(

fooSize w=2.0 h=3.0

类似于 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:

NSSize fooSize = NSMakeSize(2, 3);
MACRO_NAME_HERE(fooSize);

the macro would expand to:

NSLog(@"%s w=%f h=%f", "fooSize", fooSize.width, fooSize.height);

and print:

fooSize w=2.0 h=3.0

(similar to NSStringFromSize, but with the variable name)

苹果你个爱泡泡 2024-07-25 02:43:55

# 的正式名称是字符串化运算符。 它采用其参数并将其用引号括起来以形成 C 字符串常量,并根据需要转义任何嵌入的引号或反斜杠。 它只允许在宏定义中使用——在常规代码中不允许使用。 例如:

// This is not legal C
const char *str = #test

// This is ok
#define STRINGIZE(x) #x
const char *str1 = STRINGIZE(test);      // equivalent to str1 = "test";
const char *str2 = STRINGIZE(test2"a\"");  // equivalent to str2 = "test2\"a\\\"";

相关的预处理器运算符是标记粘贴运算符 ##。 它需要两个令牌并将它们粘贴在一起以获得一个令牌。 与字符串化运算符一样,它只允许在宏定义中使用,而不能在常规代码中使用。

// This is not legal C
int foobar = 3;
int x = foo ## bar;

// This is ok
#define TOKENPASTE(x, y) x ## y
int foobar = 3;
int x = TOKENPASTE(foo, bar);  // equivalent to x = foobar;

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:

// This is not legal C
const char *str = #test

// This is ok
#define STRINGIZE(x) #x
const char *str1 = STRINGIZE(test);      // equivalent to str1 = "test";
const char *str2 = STRINGIZE(test2"a\"");  // equivalent to str2 = "test2\"a\\\"";

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.

// This is not legal C
int foobar = 3;
int x = foo ## bar;

// This is ok
#define TOKENPASTE(x, y) x ## y
int foobar = 3;
int x = TOKENPASTE(foo, bar);  // equivalent to x = foobar;
把回忆走一遍 2024-07-25 02:43:55

这是宏定义的主体吗? 然后#可用于字符串化以下标识符,即打印“字符串”(不带代码)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文