简体   繁体   English

C预处理程序优先级

[英]C Preprocessor precedence

I have the following problem: 我有以下问题:

extern void func_name(const char *f);  
#define EXPECT(f) func_name(#f)  
#define foo bar  

void main()  
{  
     EXPECT(foo);  
}  

So, the 所以

EXPECT(foo);

will be actually evaluated to 将实际评估为

func_name("foo");

What I actually need is to convert 'foo' to 'bar', ie the code to become 我真正需要的是将'foo'转换为'bar',即成为的代码

func_name("bar");

So I kind of want to do this: 所以我有点想这样做:

#define "foo" "bar"

But this does not work, because macro names must be identifiers. 但这不起作用,因为宏名称必须是标识符。 I also tried to find a way to change the preprocessor precedence, so my macro will be replaced first, but didn't find a way. 我还试图找到一种方法来改变预处理器的优先级,所以我的宏将首先被替换,但没有找到方法。

The perfect solution will not change main() at all. 完美的解决方案根本不会改变main()。

#define S(x) #x
#define EXPECT(f) func_name(S(f))  

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

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