C++基础 数据类型占字节大小分析

时间:2022-07-22
本文章向大家介绍C++基础 数据类型占字节大小分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

概述

C++基本数据类型

类型

关键字

布尔型

bool

字符型

char

整型

int

浮点型

float

双浮点型

double

无类型

void

宽字符型

wchar_t

一些基本类型可以使用一个或多个类型修饰符进行修饰:

  • signed
  • unsigned
  • short
  • long

各数据类型在内存中所占字节的大小随系统的差异而变,可通过sizeof()函数查看,下面直接总结一些基本类型所占字节的大小。

所占字节大小总结

类型

32位系统

64位系统

bool

1

1

char

1

1

short

2

2

int

4

4

long

4

4

double

8

8

float

4

4

*

4

8

注:有的地方说64位系统中long占8字节,不过在我的电脑中实测确是4字节,所以还是要根据实际的硬件及编译器确定到底占用多少!

测试程序

几点说明:

  • signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
  • 但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。 如果用于文本,则使用未加限定的char, 是类似于 'a', '0'的类型, 或是组成C字符串"abcde"的类型。它也可以是一个值,但是是当做无符号还是有符号数没有指定。如果用于数字,那么: signed char, 范围 -128 到 127,unsigned char, 范围 0 ~ 255。
  • signed与signed int与int是等价类型
#include<iostream>  
#include<string>  
#include <limits>  
using namespace std;

int main()
{
    /*
    signed, unsigned, long和short都隐含了int, 等价于signed int, unsigned int, long int, short int
    但注意char没有这种默认等价性。char不和signed char或者unsigned char其中任何一个等价。使用char的时候最好标明是signed char还是unsigned char。
    signed与signed int与int是等价类型
    */
    cout << "type: tt" << "************size**************" << endl;
    // *
    cout << "--------------------------------" << endl;
    int *p;
    cout << "*p: tt" << "所占字节数:" << sizeof(p) << endl;

    // bool
    cout << "--------------------------------" << endl;
    cout << "bool: tt" << "所占字节数:" << sizeof(bool);
    cout << "t最大值:" << (numeric_limits<bool>::max)();
    cout << "tt最小值:" << (numeric_limits<bool>::min)() << endl;

    // char
    cout << "--------------------------------" << endl;
    cout << "char: tt" << "所占字节数:" << sizeof(char);
    cout << "t最大值:" << (numeric_limits<char>::max)();
    cout << "tt最小值:" << (numeric_limits<char>::min)() << endl;

    cout << "signed char: t" << "所占字节数:" << sizeof(signed char);
    cout << "t最大值:" << (numeric_limits<signed char>::max)();
    cout << "tt最小值:" << (numeric_limits<signed char>::min)() << endl;

    cout << "unsigned char: t" << "所占字节数:" << sizeof(unsigned char);
    cout << "t最大值:" << (numeric_limits<unsigned char>::max)();
    cout << "tt最小值:" << (numeric_limits<unsigned char>::min)() << endl;

    // short (typedef short int wchar_t;)
    cout << "--------------------------------" << endl;
    cout << "short: tt" << "所占字节数:" << sizeof(short);
    cout << "t最大值:" << (numeric_limits<short>::max)();
    cout << "tt最小值:" << (numeric_limits<short>::min)() << endl;

    cout << "unsigned short:t" << "所占字节数:" << sizeof(unsigned short);
    cout << "t最大值:" << (numeric_limits<unsigned short>::max)();
    cout << "tt最小值:" << (numeric_limits<unsigned short>::min)() << endl;

    cout << "wchar_t: t" << "所占字节数:" << sizeof(wchar_t);
    cout << "t最大值:" << (numeric_limits<wchar_t>::max)();
    cout << "tt最小值:" << (numeric_limits<wchar_t>::min)() << endl;

    // int
    cout << "--------------------------------" << endl;
    cout << "int: tt" << "所占字节数:" << sizeof(int);
    cout << "t最大值:" << (numeric_limits<int>::max)();
    cout << "t最小值:" << (numeric_limits<int>::min)() << endl;

    cout << "unsigned: t" << "所占字节数:" << sizeof(unsigned);
    cout << "t最大值:" << (numeric_limits<unsigned>::max)();
    cout << "t最小值:" << (numeric_limits<unsigned>::min)() << endl;

    // long
    cout << "--------------------------------" << endl;
    cout << "long: tt" << "所占字节数:" << sizeof(long);
    cout << "t最大值:" << (numeric_limits<long>::max)();
    cout << "t最小值:" << (numeric_limits<long>::min)() << endl;

    cout << "unsigned long: t" << "所占字节数:" << sizeof(unsigned long);
    cout << "t最大值:" << (numeric_limits<unsigned long>::max)();
    cout << "t最小值:" << (numeric_limits<unsigned long>::min)() << endl;

    // double
    cout << "--------------------------------" << endl;
    cout << "double: t" << "所占字节数:" << sizeof(double);
    cout << "t最大值:" << (numeric_limits<double>::max)();
    cout << "t最小值:" << (numeric_limits<double>::min)() << endl;

    cout << "long double: t" << "所占字节数:" << sizeof(long double);
    cout << "t最大值:" << (numeric_limits<long double>::max)();
    cout << "t最小值:" << (numeric_limits<long double>::min)() << endl;

    // float
    cout << "--------------------------------" << endl;
    cout << "float: tt" << "所占字节数:" << sizeof(float);
    cout << "t最大值:" << (numeric_limits<float>::max)();
    cout << "t最小值:" << (numeric_limits<float>::min)() << endl;

    cout << "size_t: t" << "所占字节数:" << sizeof(size_t);
    cout << "t最大值:" << (numeric_limits<size_t>::max)();
    cout << "t最小值:" << (numeric_limits<size_t>::min)() << endl;

    cout << "string: t" << "所占字节数:" << sizeof(string) << endl;
    // << "t最大值:" << (numeric_limits<string>::max)() << "t最小值:" << (numeric_limits<string>::min)() << endl;  
    
    cout << "type: tt" << "************size**************" << endl;
    
    return 0;
}

64位编译器结果(Visual Studio x64)

type:           ************size**************
--------------------------------
*p:             所占字节数:8
--------------------------------
bool:           所占字节数:1   最大值:1               最小值:0
--------------------------------
char:           所占字节数:1   最大值:                最小值:€
signed char:    所占字节数:1   最大值:                最小值:€
unsigned char:  所占字节数:1   最大值:              最小值:
--------------------------------
short:          所占字节数:2   最大值:32767           最小值:-32768
unsigned short: 所占字节数:2   最大值:65535           最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
--------------------------------
int:            所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned:       所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
long:           所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned long:  所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
double:         所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
long double:    所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
--------------------------------
float:          所占字节数:4   最大值:3.40282e+38     最小值:1.17549e-38
size_t:         所占字节数:8   最大值:18446744073709551615    最小值:0
string:         所占字节数:40
type:           ************size**************

32位编译器结果(Visual Studio x86)

type:           ************size**************
--------------------------------
*p:             所占字节数:4
--------------------------------
bool:           所占字节数:1   最大值:1               最小值:0
--------------------------------
char:           所占字节数:1   最大值:                最小值:€
signed char:    所占字节数:1   最大值:                最小值:€
unsigned char:  所占字节数:1   最大值:              最小值:
--------------------------------
short:          所占字节数:2   最大值:32767           最小值:-32768
unsigned short: 所占字节数:2   最大值:65535           最小值:0
wchar_t:        所占字节数:2   最大值:65535           最小值:0
--------------------------------
int:            所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned:       所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
long:           所占字节数:4   最大值:2147483647      最小值:-2147483648
unsigned long:  所占字节数:4   最大值:4294967295      最小值:0
--------------------------------
double:         所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
long double:    所占字节数:8   最大值:1.79769e+308    最小值:2.22507e-308
--------------------------------
float:          所占字节数:4   最大值:3.40282e+38     最小值:1.17549e-38
size_t:         所占字节数:4   最大值:4294967295      最小值:0
string:         所占字节数:28
type:           ************size**************

参考:https://www.runoob.com/cplusplus/cpp-data-types.html