各个平台 基本数据类型 长度

发布时间:2016-4-15 18:25
分类名称:C++


由于各个平台和编译的不同,会导致各种数据类型不一致的情况,C++标准只给出了最小长度:
各个平台 基本数据类型 长度 - Howie - Dspace

C++标准规定:一个int至少和一个short一样大,一个long至少和一个int一样大,一个long long至少和一个long一样大。其中long long是c++11新定义的。

示例代码:

#include <stdio.h>
#include <iostream>

using namespace std;
int main()
{
        cout << "bool " << sizeof(bool) << endl;

        cout << "char " << sizeof(char) << endl;
        cout << "wchar_t " << sizeof(wchar_t) << endl;
        cout << "char16_t " << sizeof(char16_t) << endl;
        cout << "char32_t " << sizeof(char32_t) << endl;

        cout << "short " << sizeof(short) << endl;
        cout << "int " << sizeof(int) << endl;
        cout << "long " << sizeof(long) << endl;
        cout << "long long" << sizeof(long long) << endl;

        cout << "float " << sizeof(float) << endl;
        cout << "double " << sizeof(double) << endl;
        cout << "long double " << sizeof(long double) << endl;
        return 0;
}



 下面是各个平台的运行示例代码的结果:

===== Windows x86/x64 =====
bool 1
char 1
wchar_t 2
char16_t 2
char32_t 4
short 2
int 4
long 4
long long 8
float 4
double 8
long double 8

===== Linux_x64 =====
bool 1
char 1
wchar_t 4
char16_t 2
char32_t 4
short 2
int 4
long 8
long long 8
float 4
double 8
long double 16

===== Linux_x86 =====
bool 1
char 1
wchar_t 4
char16_t 2
char32_t 4
short 2
int 4
long 4
long long 8
float 4
double 8
long double 12

==== Mac_x64 ====
bool 1
char 1
wchar_t 4
char16_t 2
char32_t 4
short 2
int 4
long 8
long long 8
float 4
double 8
long double 16