首页 > 解决方案 > 为什么 Linux 和 Windows 之间的 long 和 long double 类型大小不同?

问题描述

我现在正在学习计算机体系结构。为什么 Linux 和 Windows 操作系统之间的数据类型大小不同?

这是我的示例 C 代码。

#include<stdio.h>
int main(void)
{
    printf ("\n-- General Data Type Size --\n");
    printf ("char size : %d byte\n", (int)sizeof(char));
    printf ("short size : %d byte\n", (int)sizeof(short));
    printf ("int size : %d byte\n", (int)sizeof(int));
    printf ("long size : %d byte\n", (int)sizeof(long));
    printf ("double size : %d byte\n", (int)sizeof(double));
    printf ("long double size : %d byte\n", (int)sizeof(long double));

    printf ("\n-- Pointer Data Type Size -- \n");
    printf ("char* size : %d byte\n", (int)sizeof(char*));
    printf ("short* size : %d byte\n", (int)sizeof(short*));
    printf ("int* size : %d byte\n", (int)sizeof(int*));
    printf ("long* size : %d byte\n", (int)sizeof(long*));
    printf ("double* size : %d byte\n", (int)sizeof(double*));
    printf ("long double* size : %d byte\n", (int)sizeof(long double*));

    return 0;
} 

当我使用visual studio 10在windwos 10 64bit上编译并运行此代码时,它显示如下:

窗口结果

然后,当我在我的 ubuntu 18.04 64 位上编译并运行这段代码时。它显示如下:

Ubuntu结果

Windows 系统显示 long double 的大小为 8 字节,long 为 4 字节。

但是 Ubuntu 向我显示 long double 的大小是 16 字节,而 long 是 8 字节。为什么两个尺寸不一样?两者都有 64 位环境,我想知道为什么它们有不同的值。如果您能解释一下,我将不胜感激。

标签: ctypescpu-architectureportabilityabi

解决方案


推荐阅读