【发布时间】:2015-08-07 12:04:25
【问题描述】:
代码:
char *color_name[] = {
"red",
"blue",
"green"
};
#define color_num (sizeof(color_name)/sizeof(char*))
int main(){
printf("size %d \n",color_num);
return 0;
}
它可以在 Centos 7 上与 GCC 4.8.2 一起正常工作。
但我在 mac 上运行上面的程序时出错:
note:expanded from macro 'color_num'
我的 Mac 上的编译器:
……include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.49) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
听说 GCC 在 Mac 上用于编译程序时已链接到 Clang,对吗?
问题:
那么为什么 Clang 会报告这个错误呢?这与预处理有关吗?
如果我这样做,它工作正常:
int a = color_num;
printf("%d\n",a);
或:
printf("%d\n",sizeof(color_num)/sizeof(char*));
UPDATA=============
Crayon_277@Macintosh 20150525$ gcc -g -o ex11 ex1.c
ex1.c:16:21: warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
printf("size %d\n",color_num);
~~ ^~~~~~~~~
%lu
ex1.c:14:19: note: expanded from macro 'color_num'
#define color_num (sizeof(color)/sizeof(char*))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
似乎没有错误,只是那个格式警告。
我认为这可能与我用于 vim 的扩展有关 scrooloose/语法
我得到了错误:
【问题讨论】:
-
如果您可以发布实际的错误消息会有所帮助。 “注释”只是为了帮助您理解错误,而不是错误消息本身。
-
这是意料之中的。实际上,gcc 也会通知这一点,但它确实需要包含在 -Wall 中的 -Wformat。后者是至少应该始终启用的(-Wextra 推荐)。
-
请注意,没有参数的函数应注明
f(void)。
标签: c gcc macros clang c-preprocessor