I have the following code:
#include<stdio.h>
int main()
{
printf("The 'int' datatype is %lu bytes
", sizeof(int));
printf("The 'unsigned int' data type is %lu bytes
", sizeof(unsigned int));
printf("The 'short int' data type is %lu bytes
", sizeof(short int));
printf("The 'long int' data type is %lu bytes
", sizeof(long int));
printf("The 'long long int' data type is %lu bytes
", sizeof(long long int));
printf("The 'float' data type is %lu bytes
", sizeof(float));
printf("The 'char' data type is %lu bytes
", sizeof(char));
}
Which outputs:
The 'int' datatype is 4 bytes
The 'unsigned int' data type is 4 bytes
The 'short int' data type is 2 bytes
The 'long int' data type is 8 bytes
The 'long long int' data type is 8 bytes
The 'float' data type is 4 bytes
The 'char' data type is 1 bytes
But that's just the thing, the compiler requires that I use %lu
(long unsigned int) rather than %d
(int), as I would have expected. After all, we are just talking about single digit numbers here, aren't we? So why do I get the following error when using %d
instead of %lu
? Has it something to do with me being on a 64bit system(Ubuntu 14.10)?
helloworld.c: In function ‘main’:
helloworld.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'int' datatype is %d bytes
", sizeof(int));
^
helloworld.c:6:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'unsigned int' data type is %d bytes
", sizeof(unsigned int));
^
helloworld.c:7:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'short int' data type is %d bytes
", sizeof(short int));
^
helloworld.c:8:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'long int' data type is %d bytes
", sizeof(long int));
^
helloworld.c:9:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'long long int' data type is %d bytes
", sizeof(long long int));
^
helloworld.c:10:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'float' data type is %d bytes
", sizeof(float));
^
helloworld.c:11:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
printf("The 'char' data type is %d bytes
", sizeof(char));
^
Compilation finished successfully.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…