我總是收到編譯警告,但我不知道如何解決它:「%d」需要類型「詮釋」的說法,但參數2的類型爲「長unsigned int類型」 [-Wformat =]
'%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [
程序運行正常,但我仍然得到編譯警告:
/* Sizeof.c--Program to tell byte size of the C variable */
#include <stdio.h>
int main(void) {
printf("\nA Char is %d bytes", sizeof(char));
printf("\nAn int is %d bytes", sizeof(int));
printf("\nA short is %d bytes", sizeof(short));
printf("\nA long is %d bytes", sizeof(long));
printf("\nA long long is %d bytes\n", sizeof(long long));
printf("\nAn unsigned Char is %d bytes", sizeof(unsigned char));
printf("\nAn unsigned int is %d bytes", sizeof(unsigned int));
printf("\nAn unsigned short is %d bytes", sizeof(unsigned short));
printf("\nAn unsigned long is %d bytes", sizeof(unsigned long));
printf("\nAn unsigned long long is %d bytes\n",
sizeof(unsigned long long));
printf("\nfloat is %d bytes", sizeof(float));
printf("\nA double is %d bytes\n", sizeof(double));
printf("\nA long double is %d bytes\n", sizeof(long double));
return 0;
}
在Windows上的GCC 4.8.1中,出現錯誤:「printf'ing%zu時格式爲」未知轉換類型字符'z'。 –
@CzarekTomczak更新了答案,可能與此有關。 –
謝謝Shafik。不幸的是,這不是跨平臺的。我必須將size_t轉換爲(unsigned long),以使代碼可以在Linux和Windows上運行。在Linux上使用%Iu(I as Integer)時出現錯誤「format'%u'期望類型爲'unsigned int'的參數」。 –