C++ 基本数据类型的宽度

本文详细探讨了C++编程语言中各种基本数据类型的宽度,包括char、int、float、double等。解释了不同平台下这些类型可能的字节数,并讨论了它们在内存中的表示和使用场景。

#include <iostream>

using namespace std;

int main()
{
    cout << "this is a demo." << endl;

    // 布尔型
    cout << "bool" << endl;
    bool _bool;
    cout << _bool << endl;
    // signed bool _signed_bool;
    // unsigned bool _unsigned_bool;
    // short bool _short_bool;
    // long bool _long_bool;
    cout << sizeof(_bool) << endl;      // 占 1 字节
    
    // 字符型
    cout << "char" << endl;
    char _char;
    signed char _signed_char;
    unsigned char _unsigned_char;
    // short char _short_char;                      
    // long char _long_char;
    cout << sizeof(_char) << endl;                // 占 1 字节
    cout << sizeof(_signed_char) << endl;        // 占 1 字节
    cout << sizeof(_unsigned_char) << endl;     // 占 1 字节
    // cout << sizeof(_short_char) << endl;    //
    // cout << sizeof(_long_char) << endl;    //


    // 宽字符型
    cout << "wchar_t" << endl;
    wchar_t _wchar_t;
    signed wchar_t _signed_wchar_t;
    unsigned wchar_t _unsigned_wchar_t;
    short wchar_t _short_wchar_t;
    long wchar_t _long_wchar_t;
    cout << sizeof(_wchar_t) << endl;                // 占 2 字节
    cout << sizeof(_signed_wchar_t) << endl;        // 占 2 字节
    cout << sizeof(_unsigned_wchar_t) << endl;     // 占 4 字节
    cout << sizeof(_short_wchar_t) << endl;       // 占 2 字节
    cout << sizeof(_long_wchar_t) << endl;       // 占 4 字节
    
    // 短整形
    cout << "short" << endl;
    short _short;
    signed short _signed_short;
    unsigned short _unsigned_short;
    // short short _short_short;
    // long short _long_short;
    cout << sizeof(_short) << endl;                  // 占 2 字节 
    cout << sizeof(_signed_short) << endl;          // 占 2 字节
    cout << sizeof(_unsigned_short) << endl;       // 占 2 字节
    // cout << sizeof(_short_short) << endl;      //
    // cout << sizeof(_long_short) << endl;      //

    // 整型
    cout << "int" << endl;
    int _int;
    signed int _signed_int;
    unsigned int _unsigned_int;
    short int _short_int;
    long int _long_int;
    cout << sizeof(_int) << endl;                 // 占 4 字节
    cout << sizeof(_signed_int) << endl;         // 占 4 字节
    cout << sizeof(_unsigned_int) << endl;      // 占 4 字节
    cout << sizeof(_short_int) << endl;        // 占 2 字节
    cout << sizeof(_long_int) << endl;        // 占 4 字节
    
    // 长整型
    cout << "long" << endl;
    long _long;
    signed long _signed_long;
    unsigned long _unsigned_long;
    // short long _short_long;
    long long _long_long;
    cout << sizeof(_long) << endl;                    // 占 4 字节
    cout << sizeof(_signed_long) << endl;            // 占 4 字节
    cout << sizeof(_unsigned_long) << endl;         // 占 8 字节
    // cout << sizeof(_short_long) << endl;        //
    cout << sizeof(_long_long) << endl;           // 占 8 字节

    // 单精度浮点型
    cout << "float" << endl;
    float _float;
    // signed float _signed_float;
    // unsigned float _unsigned_float;
    // short float _short_float;
    // long float _long_float;    
    cout << sizeof(_float) << endl;                       // 占 4 字节
    // cout << sizeof(_signed_float) << endl;            //
    // cout << sizeof(_unsigned_float) << endl;         //
    // cout << sizeof(_short_float) << endl;           //
    // cout << sizeof(_long_float) << endl;           //

    // 双精度浮点型
    cout << "double" << endl;
    double _double;
    // signed double _signed_double;
    // unsigned double _unsigned_double;
    // short double _short_double;
    long double _long_double;
    cout << sizeof(_double) << endl;                  // 占 8 字节
    // cout << sizeof(_signed_double) << endl;       //
    // cout << sizeof(_unsigned_double) << endl;    //
    // cout << sizeof(_short_double) << endl;      //
    cout << sizeof(_long_double) << endl;         // 占 12 字节

    // this is a demo.
    // bool
    // 1
    // char
    // 1
    // 1
    // 1
    // wchar_t
    // 2
    // 2
    // 4
    // 2
    // 4
    // short
    // 2
    // 2
    // 2
    // int
    // 4
    // 4
    // 4
    // 2
    // 4
    // long
    // 4
    // 4
    // 4
    // 8
    // float
    // 4
    // double
    // 8
    // 12
    
	cout << "Size of bool : " << sizeof(bool) << endl;
    cout << "Size of char : " << sizeof(char) << endl;
    cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
    cout << "Size of int : " << sizeof(int) << endl;
    cout << "Size of short int : " << sizeof(short int) << endl;
    cout << "Size of long int : " << sizeof(long int) << endl;
    cout << "Size of float : " << sizeof(float) << endl;
    cout << "Size of double : " << sizeof(double) << endl;

    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值