C++中各种基本类型的范围。

1.C++中有哪些基本类型

对于整数来说,它有有符号和无符号之分;对于实数,无论是单精度还是双精度,都是有符号的(有正有负);

  • 布尔类型;只有两值,无所谓有无符号;true|false, 1种;
  • 字符类型;包括有无符号;signed(可以省略),2种;
  • 短整型;包括有无符号;2种
  • 整型;有无符号,2种;
  • 长整型;有无符号,2种;
  • 长长整型;有无符号,2种;
  • 单精度;有符号,1种;
  • 双精度;有符号,1种;
    共计13种

2.长度

在内存分配时,无论静态还是动态,占几个字节的存储空间;用运算符sizeof获取;使用时:sizeof 变量|数据类型|字面常量; sizeof(变量|数据类型|字面常量)

拓展:
1bit(字节)=8位,例如char 字符类型是一个字节;
int 整型类型是4个字节,也就是32位;
int a = 1,a是四个字节,在计算机存储的是 00000000 00000000 00000000 00000001


范围

因为在内存中的数据是分段的,每种数据类型都有范围的,分为最小值和最大值;
下面我们写代码将所有基本类型的长度及范围打印出来,引入一个新的头文件:climits


代码示例:

#include <iostream>
#include <climits>
using namespace std;
int main() {
    //..............................................................
    cout << " ~~布尔类型~~" << endl;
    cout << " 它占: " << sizeof(bool) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<bool>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<bool>::max()) << endl;
    cout << endl;
    cout << " ~~有符号字符类型~~" << endl;
    cout << " 它占: " << sizeof(char) << " 个字节!" << endl;
    cout << " 最小值:" << (int)(numeric_limits<char>::min()) << endl;
    cout << " 最大值:" << (int)(numeric_limits<char>::max()) << endl;
    cout << endl;
    cout << " ~~无符号字符类型~~" << endl;
    cout << " 它占: " << sizeof(unsigned char) << " 个字节!" << endl;
    cout << " 最小值:" << (int)(numeric_limits<unsigned char>::min()) << endl;
    cout << " 最大值:" << (int)(numeric_limits<unsigned char>::max()) << endl;
    cout << endl;
    cout << " ~~整数之有符号短整型~~" << endl;
    cout << " 它占: " << sizeof(short) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<short>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<short>::max()) << endl;
    cout << endl;
    cout << " ~~整数之无符号短整型~~" << endl;
    cout << " 它占: " << sizeof(unsigned short) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<unsigned short>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<unsigned short>::max()) << endl;
    cout << endl;
    cout << " ~~整数之有符号整型~~" << endl;
    cout << " 它占: " << sizeof(int) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<int>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<int>::max()) << endl;
    cout << endl;
    cout << " ~~整数之无符号整型~~" << endl;
    cout << " 它占: " << sizeof(unsigned int) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<unsigned int>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<unsigned int>::max()) << endl;
    cout << endl;
    cout << " ~~整数之有符号长整型~~" << endl;
    cout << " 它占: " << sizeof(long) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<long int>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<long int>::max()) << endl;
    cout << endl;
    cout << " ~~整数之无符号长整型~~" << endl;
    cout << " 它占: " << sizeof(unsigned long) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<unsigned long>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<unsigned long>::max()) << endl;
    cout << endl;
    cout << " ~~整数之有符号长长整型~~" << endl;
    cout << " 它占: " << sizeof(long long) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<long long>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<long long>::max()) << endl;
    cout << endl;
    cout << " ~~整数之无符号长长整型~~" << endl;
    cout << " 它占: " << sizeof(unsigned long long) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<unsigned long long>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<unsigned long long>::max()) << endl;
    cout << "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *" << endl;
    cout << " ~~实数之单精度,都是有符号~~" << endl;
    cout << " 它占: " << sizeof(float) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<float>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<float>::max()) << endl;
    cout << endl;
    cout << " ~~实数之双精度,是有符号~~" << endl;
    cout << " 它占: " << sizeof(double) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<double>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<double>::max()) << endl;
    cout << endl;
    cout << " ~~实数之长双精度,是有符号~~" << endl;
    cout << " 它占: " << sizeof(long double) << " 个字节!" << endl;
    cout << " 最小值:" << (numeric_limits<long double>::min()) << endl;
    cout << " 最大值:" << (numeric_limits<long double>::max()) << endl;
    cout << endl;
    //..............................................................
    return 0;
}

运行截图:
在这里插入图片描述
在这里插入图片描述

拓展:

在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暴躁茹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值