C 预处理器,将宏的结果字符串化
我想将宏扩展的结果字符串化。
我尝试过以下操作:
#define QUOTE(str) #str
#define TEST thisisatest
#define TESTE QUOTE(TEST)
TESTE 扩展为:“TEST”,而我试图获取“thisisatest”。我知道这是预处理器的正确行为,但是任何人都可以帮助我找到实现另一个的方法吗?
Using TESTE #TEST is not valid
Using TESTE QUOTE(thisisatest) is not what I'm trying to do
I want to stringify the result of a macro expansion.
I've tried with the following:
#define QUOTE(str) #str
#define TEST thisisatest
#define TESTE QUOTE(TEST)
And TESTE gets expanded to: "TEST", while I'm trying to get "thisisatest". I know this is the correct behavior of the preprocessor but can anyone help me with a way to achieve the other one?
Using TESTE #TEST is not valid
Using TESTE QUOTE(thisisatest) is not what I'm trying to do
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样:
原因是当宏参数被替换到宏体中时,它们会被扩展,除非它们与 # 或 ## 预处理器运算符一起出现在该宏中。因此,
str
(代码中值为TEST
)不会在QUOTE
中展开,但会在EXPAND_AND_QUOTE
中展开代码>.Like this:
The reason is that when macro arguments are substituted into the macro body, they are expanded unless they appear with the # or ## preprocessor operators in that macro. So,
str
(with valueTEST
in your code) isn't expanded inQUOTE
, but it is expanded inEXPAND_AND_QUOTE
.为了澄清一点,本质上预处理器是为了执行另一个“阶段”。即:
第一种情况:
第二种情况:
To clarify a bit more, essentially the preprocessor was made to execute another "stage". i.e :
1st case:
2nd case: