为什么 long double 在一个字符串中只占用 10 个字节? [英] Why does a long double take up only 10 bytes in a string?

查看:24
本文介绍了为什么 long double 在一个字符串中只占用 10 个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:long double 的大小是 16 bytes,当我用 sizeof() 检查时,没有问题,它返回 16 但我做了这个小程序,使用 long double 强制转换用零填充 16 字节字符串,但它只填充 10 字节的零而不是 16,只是比longdouble多了两个,为什么会这样?

I have a question: the size of a long double is 16 bytes, when I check that with sizeof() there is no problem, it returns 16 but I did this little program to fill a 16 byte string with zeros using a long double cast, but it fills only 10 bytes of zeros instead of 16, just two more than long and double, why does this happen?

#include <stdio.h>

int main(void)
{
    char s[17] = "aaaaaaaaaaaaaaaa";

    *((long double *)s) = 0.L;
    for (int i = 0; i < 16; i++)
        printf("%d", (int)s[i]);
    write(1, "\n", 1);
    return (0);
}

输出:

0000000000979797979797

代替:

0000000000000000

对于 double 它输出很好:

000000009797979797979797

那么为什么它适用于 double 而不是 long double?

So why does it work with double but not long double?

推荐答案

那是因为大多数实现仅使用 80 位(10 字节) 来表示 long double 表示(来自维基百科):

That's because most implementations use only 80-bit (10 bytes) for long double representation (from Wikipedia):

在 x86 架构上,大多数 C 编译器都实现了 long double 作为 x86 硬件支持的 80 位扩展精度类型(通常存储为 12 或 16 字节以保持数据结构对齐),如 C99/C11 标准(IEC 60559 浮点运算(附录 F)).

这篇关于为什么 long double 在一个字符串中只占用 10 个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆