在C宏中#x是什么意思?

时间:2022-05-03 22:31:22

For example I have a macro:

例如,我有一个宏:

#define PRINT(int) printf(#int "%d\n",int)

I kinda know what is the result. But how come #int repersent the whole thing?

我知道结果如何。但是整件事是怎么回事呢?

I kinda forget this detail. Can anybody kindely give me a hint?

我忘记了这个细节。有人能给我个提示吗?

Thanks!

谢谢!

3 个解决方案

#1


31  

In this context (applied to a parameter reference in a macro definition), the pound sign means to expand this parameter to the literal text of the argument that was passed to the macro.

在此上下文中(应用于宏定义中的参数引用),磅符号表示将该参数扩展到传递给宏的参数的文本文本。

In this case, if you call PRINT(5) the macro expansion will be printf("5" "%d\n", 5); which will print 5 5; not very useful; however if you call PRINT(5+5) the macro expansion will be printf("5+5" "%d\n", 5+5); which will print 5+5 10, a little less trivial.

在这种情况下,如果您调用PRINT(5),宏扩展将是printf(“5”“%d\n”,5);打印5个5;不是非常有用;但是如果您调用PRINT(5+5),宏扩展将是printf(“5+5”“%d\n”,5+5);打印5+5 10,有点小。

This very example is explained in this tutorial on the C preprocessor (which, incidentally, is the first Google hit for c macro pound sign).

这个例子在本教程中解释了C预处理器(顺便说一下,这是C宏磅符号的第一个谷歌命中)。

#2


10  

"#" can show the name of a variable, it's better to define the macro as this:

“#”可以显示变量的名称,最好将宏定义为:

#define PRINT(i) printf(#i "= %d\n", i)

and use it like this:

然后像这样使用:

int i = 5;
PRINT(i);

Result shown:

结果显示:

i = 5

#3


5  

That is a bad choice of name for the macro parameter, but harmless (thanks dreamlax).

对于宏参数来说,这是一个糟糕的名称选择,但是无害(感谢dreamlax)。

Basically if i write like so

基本上如果我这样写。

PRINT(5);

It will be replaced as

它将被替换为。

printf("5" "%d\n",5);

or

printf("5 %d\n",5);

It is a process called Stringification, #int is replaced with a string consisting of its content, 5 -> "5"

它是一个称为Stringification的过程,#int被替换为一个包含其内容的字符串,5 ->“5”

#1


31  

In this context (applied to a parameter reference in a macro definition), the pound sign means to expand this parameter to the literal text of the argument that was passed to the macro.

在此上下文中(应用于宏定义中的参数引用),磅符号表示将该参数扩展到传递给宏的参数的文本文本。

In this case, if you call PRINT(5) the macro expansion will be printf("5" "%d\n", 5); which will print 5 5; not very useful; however if you call PRINT(5+5) the macro expansion will be printf("5+5" "%d\n", 5+5); which will print 5+5 10, a little less trivial.

在这种情况下,如果您调用PRINT(5),宏扩展将是printf(“5”“%d\n”,5);打印5个5;不是非常有用;但是如果您调用PRINT(5+5),宏扩展将是printf(“5+5”“%d\n”,5+5);打印5+5 10,有点小。

This very example is explained in this tutorial on the C preprocessor (which, incidentally, is the first Google hit for c macro pound sign).

这个例子在本教程中解释了C预处理器(顺便说一下,这是C宏磅符号的第一个谷歌命中)。

#2


10  

"#" can show the name of a variable, it's better to define the macro as this:

“#”可以显示变量的名称,最好将宏定义为:

#define PRINT(i) printf(#i "= %d\n", i)

and use it like this:

然后像这样使用:

int i = 5;
PRINT(i);

Result shown:

结果显示:

i = 5

#3


5  

That is a bad choice of name for the macro parameter, but harmless (thanks dreamlax).

对于宏参数来说,这是一个糟糕的名称选择,但是无害(感谢dreamlax)。

Basically if i write like so

基本上如果我这样写。

PRINT(5);

It will be replaced as

它将被替换为。

printf("5" "%d\n",5);

or

printf("5 %d\n",5);

It is a process called Stringification, #int is replaced with a string consisting of its content, 5 -> "5"

它是一个称为Stringification的过程,#int被替换为一个包含其内容的字符串,5 ->“5”