简体   繁体   English

宏作为C中宏的参数

[英]Macro as an argument to a macro in C

I want to know which macro gets replaced first in the following code 我想知道下面的代码首先替换了哪个宏

#define A 100
#define B 200
#define C(A,B) A+B

here when we use C, then evaluation will be from left to right or right to left. 在这里,当我们使用C时,评估将是从左到右或从右到左。 That is B gets the value first or A gets the value first 那就是B首先获得值或A首先获得值

i gave this example just to make things look simple, may be i was wrong. 我举这个例子只是为了使事情看起来简单,也许是我错了。 the actual thing i want to ask is, if A and B also take arguments and have the scope of expansion, then which one would expand first 我要问的实际问题是,如果A和B也都接受参数并具有扩展范围,那么哪个将首先扩展

I'm not sure what you mean. 我不确定你是什么意思。 There's never a point where you can "see" half a result of the preprocessor; 您永远不会“看到”预处理器的一半结果。 the entire input file is preprocessed, then handed over to the compiler. 整个输入文件都经过预处理,然后移交给编译器。

I think that the names for the macro arguments will never also be replaced as if they were stand-alone symbols. 我认为宏参数的名称永远也不会被替换,就好像它们是独立的符号一样。

I tried it, and this program: 我试过了,这个程序:

#include <stdio.h>

#define A 100
#define B 200
#define C(A, B) A + B

int main(void) {
  printf("A=%d\nB=%d\nC(1,2)=%d\n", A, B, C(1,2));
  return 0;
}

prints 版画

A=100
B=200
C(1,2)=3

So, C(1,2) expands to 1 + 2 , the definitions of A and B don't matter. 因此, C(1,2)扩展为1 + 2AB的定义无关紧要。

Of course I must say that I find the above very bad practice , since it's quite confusing. 当然,我必须说,我发现上述做法非常糟糕 ,因为这很令人困惑。 Never use all-caps names for macro arguments, since macros and preprocessor symbols tend to use such names. 切勿对宏参数使用全大写的名称,因为宏和预处理器符号倾向于使用此类名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM