【发布时间】:2014-10-19 05:23:20
【问题描述】:
#include <stdlib.h>
#include <stdio.h>
#define SIZE_TYPE
#define MEM_SIZE_BYTES 4096
#define MEM_SIZE_WORDS MEM_SIZE_BYTES/sizeof(int)
int main() {
printf("bytes are %d\n", MEM_SIZE_BYTES);
printf("words are %d\n", MEM_SIZE_WORDS);
}
编译给出警告...为什么?
testintsize.c:11:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat]
我在这里找到了 SIZE_TYPE 宏:https://gcc.gnu.org/onlinedocs/gccint/Type-Layout.html 和这个有关系吗?
这样做并没有导致警告消失:
#define SIZE_TYPE int
【问题讨论】:
-
sizeof运算符返回一个size_t类型的值,它必须是无符号的并且足够大以适应平台上所有可能的大小,并且在您的特定平台上是unsigned long int你需要"%lu"格式。参见例如thisprintfreference page。 -
SIZE_TYPE 宏有什么作用?
-
编译器可能会检查
size_t的定义。定义它可能会导致奇怪的事情发生。 -
与您的问题无关:请用括号括住任何比单个数字或标识符更复杂的表达式的宏。
标签: c