I have a C program below:
我有一个C程序如下:
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
when I run just the preprocessor it expands this as
当我只运行预处理器时,它将其扩展为
{
int var12=100;
printf("%d",var12);
}
which is the reason why the output is 100.
这就是输出为100的原因。
Can anybody tell me how/why the preprocessor expands var##12 to var12
?
任何人都可以告诉我预处理器如何/为什么将var ## 12扩展为var12?
4 个解决方案
#1
22
nothing too fancy: ##
tells the preprocessor to concatenate the left and right sides
没有什么太花哨的:##告诉预处理器连接左侧和右侧
see http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation
#2
5
because ## is a token concatenation operator for the c preprocessor.
因为##是c预处理器的标记连接运算符。
Or maybe I don't understand the question.
或许我不明白这个问题。
#3
4
##
is Token Pasting Operator
##是令牌粘贴操作员
The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.
双数字符号或“令牌粘贴”运算符(##),有时称为“合并”运算符,用于类似对象和类函数的宏。它允许将单独的标记连接到单个标记中,因此不能是宏定义中的第一个或最后一个标记。
If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.
如果宏定义中的形式参数在令牌粘贴运算符之前或之后,则形式参数将立即替换为未展开的实际参数。在更换之前不对参数执行宏扩展。
#4
2
#define f(g,g2) g##g2
#define f(g,g2)g ## g2
## is usued to concatenate two macros in c-preprocessor. So before compiling f(var,12) should replace by the preprocessor with var12 and hence you got the output.
##用于连接c-preprocessor中的两个宏。所以在编译之前f(var,12)应该用预处理器替换为var12,因此你得到了输出。
#1
22
nothing too fancy: ##
tells the preprocessor to concatenate the left and right sides
没有什么太花哨的:##告诉预处理器连接左侧和右侧
see http://en.wikipedia.org/wiki/C_preprocessor#Token_concatenation
#2
5
because ## is a token concatenation operator for the c preprocessor.
因为##是c预处理器的标记连接运算符。
Or maybe I don't understand the question.
或许我不明白这个问题。
#3
4
##
is Token Pasting Operator
##是令牌粘贴操作员
The double-number-sign or "token-pasting" operator (##), which is sometimes called the "merging" operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token and therefore cannot be the first or last token in the macro definition.
双数字符号或“令牌粘贴”运算符(##),有时称为“合并”运算符,用于类似对象和类函数的宏。它允许将单独的标记连接到单个标记中,因此不能是宏定义中的第一个或最后一个标记。
If a formal parameter in a macro definition is preceded or followed by the token-pasting operator, the formal parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not performed on the argument prior to replacement.
如果宏定义中的形式参数在令牌粘贴运算符之前或之后,则形式参数将立即替换为未展开的实际参数。在更换之前不对参数执行宏扩展。
#4
2
#define f(g,g2) g##g2
#define f(g,g2)g ## g2
## is usued to concatenate two macros in c-preprocessor. So before compiling f(var,12) should replace by the preprocessor with var12 and hence you got the output.
##用于连接c-preprocessor中的两个宏。所以在编译之前f(var,12)应该用预处理器替换为var12,因此你得到了输出。