C/C++ 各类型int、long、double、char、long long取值范围(基本类型的最大最小值)

做题的时候经常会使用到数据类型的最大最小值(如int, long, long long, char等),我也查了很多次,这次就记下来当笔记吧。

参考了C++ prime plus、各个博客、教程和c++官网,对C/C++中各个类型int、long、double、char、long long等基本类型的取值范围即最大最小值总结如下:

在这里插入图片描述
1字节 = 8位,“位”是计算机内存的基本单元

注意:
不同的操作系统和编译器会导致变量的取值范围不同,各位可以使用以下代码查看自己操作系统的变量取值范围:

#include<iostream>  
#include<string>  
#include <limits>  
using namespace std;  
  
int main(){  
    cout << "[type] \t\t" << "[所占字节数]\t[最大值]\t\t[最小值]"<< endl;  
    cout << "bool: \t\t" <<  sizeof(bool) << "\t\t" << (numeric_limits<bool>::max)() << "\t\t\t" << (numeric_limits<bool>::min)() << endl;  
    cout << "char: \t\t" <<  sizeof(char) << "\t\t" << CHAR_MAX << "\t\t\t" << CHAR_MIN << endl;  
    cout << "signed char: \t"  << sizeof(signed char) << "\t\t" << SCHAR_MAX << "\t\t\t" << SCHAR_MIN << endl;  
    cout << "unsigned char: \t" <<  sizeof(unsigned char) << "\t\t" << UCHAR_MAX << "\t\t\t" << (numeric_limits<unsigned char>::min)() << endl;  
    cout << "wchar_t: \t" << sizeof(wchar_t) << "\t\t" << (numeric_limits<wchar_t>::max)() << "\t\t\t" << (numeric_limits<wchar_t>::min)() << endl;  
    cout << "short: \t\t" <<  sizeof(short) << "\t\t" << SHRT_MAX << "\t\t\t" << SHRT_MIN << endl;  
    cout << "int: \t\t" <<  sizeof(int) << "\t\t" << INT_MAX << "\t\t" << INT_MIN << endl;  
    cout << "unsigned: \t" << sizeof(unsigned) << "\t\t" << UINT_MAX << "\t\t" << (numeric_limits<unsigned>::min)() << endl;  
    cout << "long: \t\t" << sizeof(long) << "\t\t" << LONG_MAX << "\t\t" << LONG_MIN << endl;  
    cout << "unsigned long: \t" << sizeof(unsigned long) << "\t\t" << ULONG_MAX << "\t\t" << (numeric_limits<unsigned long>::min)() << endl;  
    cout << "long long: \t" <<  sizeof(long long) << "\t\t" << LLONG_MAX << "\t" << LLONG_MIN << endl;  
    cout << "double: \t" << sizeof(double) << "\t\t" << (numeric_limits<double>::max)() << "\t\t" << (numeric_limits<double>::min)() << endl;
	 cout << "long double: \t" <<  sizeof(long double) << "\t\t" << (numeric_limits<long double>::max)() << "\t\t" << (numeric_limits<long double>::min)() << endl;    
    cout << "float: \t\t" <<  sizeof(float) << "\t\t" << (numeric_limits<float>::max)() << "\t\t" << (numeric_limits<float>::min)() << endl;  
    cout << "size_t: \t" << sizeof(size_t) << "\t\t" << (numeric_limits<size_t>::max)() << "\t\t" << (numeric_limits<size_t>::min)() << endl;  
    cout << "string: \t" << sizeof(string) << endl;  
    cout << "[type] \t\t" << "[所占字节数]\t[最大值]\t\t[最小值]"<< endl;   
    return 0;  
}

该代码是我查看了其他文章和教程的代码后写的,我个人认为那些代码有些乱且有错,同时我也加上了他们都漏掉的long long数据类型的取值范围。

接下来是C++官网的宏定义数据表,作为参考:
头文件:#include <climits>#include <limits.h>
在这里插入图片描述

教程中的表:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值