#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