Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does 'long double' occupy 16 bytes / 128 bits of storage when my CPU (X64 ISA)?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading