展开引号字符串内的宏 [英] Expand macros inside quoted string
问题描述
可能重复:结果
C宏创建的字符串
我有一个接受的类型的char *
的一个参数,比如 F A功能(字符串);
结果
如果字符串参数是通过即时函数调用定义,怎么能宏在字符串体内扩大呢?
I have a function which accepts one argument of type char*
, like f("string");
If the string argument is defined by-the-fly in the function call, how can macros be expanded within the string body?
例如:
#define COLOR #00ff00
f("abc COLOR");
将相当于
f("abc #00ff00");
而是不进行扩张,而且功能字面上接收 ABC COLOR
。
在我特别需要扩大宏观准确 \\#00FF00 \\
,使这一报价令牌与传递给字符串参数的其余部分连接起来 F()
,报价包括在内;也就是说,preprocessor必须完成他的工作,并欢迎编译器将来自的code F(ABC COLOR);
到 F(ABC \\#00FF00 \\);
In particular, I need to expand the macro to exactly \"#00ff00\"
, so that this quoted token is concatenated with the rest of the string argument passed to f()
, quotes included; that is, the preprocessor has to finish his job and welcome the compiler transforming the code from f("abc COLOR");
to f("abc \"#00ff00\"");
推荐答案
您不能展开在字符串中宏,但你可以写
You can't expand macros in strings, but you can write
#define COLOR "#00ff00"
f("abc "COLOR);
请记住,这级联是由C preprocessor做的,仅仅是简单的拼接字符串,而不是变量左右的功能。
Remember that this concatenation is done by the C preprocessor, and is only a feature to concatenate plain strings, not variables or so.
这篇关于展开引号字符串内的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!