栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

2.长度

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


范围

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


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

运行截图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/833057.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号