Size of short, int, long, float, double long, double, char
13,611
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 e.g. printf("%zu\n", sizeof(float));
, etc.
Author by
賴言厚
Updated on June 04, 2022Comments
-
賴言厚 almost 2 years
I want to find out the (short / int / long / float / double / long double / char) size, so I wrote this code:
printf("short\t\t%d\n", sizeof(short)); printf("int\t\t%d\n", sizeof(int)); printf("long\t\t%d\n", sizeof(long)); printf("float\t\t%d\n", sizeof(float)); printf("double\t\t%d\n", sizeof(double)); printf("long double\t%d\n", sizeof(long double)); printf("char\t\t%d\n", 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, ...?