Size of short, int, long, float, double long, double, char
I want to find out the (short / int / long / float / double / long double / char) size, so I wrote this code:
printf("shorttt%dn", sizeof(short));
printf("inttt%dn", sizeof(int));
printf("longtt%dn", sizeof(long));
printf("floattt%dn", sizeof(float));
printf("doublett%dn", sizeof(double));
printf("long doublet%dn", sizeof(long double));
printf("chartt%dn", sizeof(char));
BUT the output is:
type bytes
short 512
int 512
long 1024
float 1024
double 1024
long double 1024
char 256
Why are the number of bytes so large?
Shouldn't it be 2, 8, ...?
sizeof
evaluates to a size_t
, not an int
. So %d
is not appropriate; you're invoking undefined behaviour.
To correctly print data of type size_t
, I suggest eg printf("%zun", sizeof(float));
, etc.
Use : format specifier long unsigned int. it will work fine. Ex: printf("sizeof(long double)= % lu n", sizeof(long double));
Ans:16
链接地址: http://www.djcxy.com/p/58336.html