#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”'格式。参见例如[这个'printf'参考页面](http://en.cppreference.com/w/c/io/fprintf)。 – 2014-10-19 05:25:40
SIZE_TYPE宏有什么作用? – abc 2014-10-19 05:34:36
编译器可能会检查它是否有'size_t'的定义。定义它可能会导致奇怪的事情发生。 – 2014-10-19 05:38:27