【发布时间】:2018-05-29 14:32:25
【问题描述】:
我想知道是否可以使用 C 预处理定义来更改字符串格式说明符。我尝试编写以下内容,但似乎遇到了编译器错误。它只是试图用正确的格式说明符替换现有的格式说明符。
#include <stdio.h>
//This is the problem line....
#define %d %llu
int main(int argc, char** argv){
unsigned long long int myInt = 0;
printf("myInt start value: %d", myInt++);
printf("myInt value=%d (that got incremented)", myInt++);
printf("myInt value: %d; wow, another post-increment", myInt++);
printf("myInt final value %d", myInt);
return 0;
}
我收到以下编译器错误:
error: expected an identifier
#define %d %llu
^
为什么这种语法不可接受?有没有可能实现?
【问题讨论】:
-
1.您不能用
%d命名宏——它不是合法的C 标识符。 2. 即使可以 - 宏也不会在 C 字符串文字中替换。 3. 对于int类型的每个变体,都有匹配的格式化程序,它们在相应的位置上被正确解释。平台。因此,甚至不需要这个宏技巧。 -
宏不是这样工作的。除了遵守与 C 相同的符号名称规则外,您还可以在字符串文字中进行宏替换。
-
旁白:
%lli是unsigned long long的错误格式说明符。应该是%llu。
标签: c string format preprocessor specifier