我正在使用以下命令编译以下代码:
gcc test.c -D HEX=0xFFFF
#include <stdio.h>
#define NOERR 0
#define ERR 1
/*
* Some Code
*/
main()
{
printf(#HEX);
}
我得到以下输出:
Ex2_03.c:33:9: error: stray ‘#’ in program
printf(#HEX);
^
Ex2_03.c:33:2: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default]
printf(#HEX);
^
In file included from Ex2_03.c:1:0:
/usr/include/stdio.h:362:12: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
extern int printf (const char *__restrict __format, ...);
^
Ex2_03.c:33:2: warning: format not a string literal and no format arguments [-Wformat-security]
printf(#HEX);
^
最佳答案
你得到一个错误,因为 #
运算符只能在预处理器指令中使用。
您需要字符串化您的宏。字符串化需要一个两级宏:
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
printf(STRINGIFY(HEX));
关于c - 如何使用 -D 选项将参数中的字符串化宏传递给 gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28262502/