I’m a novice C programmer; this might be a naive question to ask but please bear with me. I learnt that the storage size of char, short, int, long, long long, float, double, and long double are implementation dependent and can vary greatly. When I used sizeof(long) on my machine, it returned me 8 bytes, but when used the same function upon long long it returned 8 bytes as well. Therefore I thought, since my CPU is based on X64 ISA, it might only support 64 bits / 8 bytes at max. But when I used the same function for long double the result was 16 bytes.
How come long double can be 16 bytes wide but not long long? If long long is same as long what’s the point of having it? And one more question, how does my CPU truly handle long double with 128 bits wide when the registers’ size of my CPU is 64 bits?
Additional Information: I use gcc (GCC) 12.2.0, if it has something to do with the compiler.
#include <stdio.h>
int main(void) {
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(long long));
printf("%d\n", sizeof(long double));
return 0;
}
The result was as follow:
8
8
16
>Solution :
How come long double can be 16 bytes wide but not long long?
… how does my CPU truly handle long double with 128 bits wide when the registers’ size of my CPU is 64 bits?
OP has self-answered.
storage size of … long long, … and long double are implementation dependent and can vary greatly
There is no fixed relationship between long long and long double in C. The CPU register size only guides implementation details, not drives them. An 8-bit CPU could have a 256-bit long double.
Enable all compiler warnings and use "%zu" when printing size_t objects.
// printf("%d\n", sizeof(long double));
printf("%zu\n", sizeof(long double));