C、C++数据类型(int, long, short, char, long long) 的取值范围、最大最小值:climits 里的一些宏

本文详细介绍了C++中各种基本数据类型及其占用字节数、取值范围等内容,包括整型、浮点型等,并对比了不同类型的大小及取值范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <climits>

using namespace std;

int main()
{

    cout << "int is "       <<sizeof(int)       << " bytes." << endl;
    cout << "short is "     <<sizeof(short)     << " bytes." << endl;
    cout << "long is "      <<sizeof(long)      << " bytes." << endl;
    cout << "long long is " <<sizeof(long long) << " bytes." << endl;
    cout << endl;


    cout << "*****Maximum values:*****" << endl;
    cout << "int: "                 << INT_MAX          << endl;
    cout << "short: "               << SHRT_MAX         << endl;
    cout << "long: "                << LONG_MAX         << endl;
    cout << "long long: "           << LLONG_MAX        << endl;
    cout << "long long: "           << LONG_LONG_MAX    << endl;//和上一个相同
    cout << "char: "                << CHAR_MAX         << endl;
    cout << "signed char: "         << SCHAR_MAX        << endl;
    cout << "unsigned char: "       << CHAR_MAX         << endl;
    cout << "unsigned short: "      << USHRT_MAX        << endl;
//    cout << "unsigned int: "        << UNIT_MAX         << endl;//报错
    cout << "unsigned long: "       << ULONG_MAX        << endl;
    cout << "unsigned long long: "  << ULLONG_MAX       << endl;
    cout << "unsigned long long: "  << ULONG_LONG_MAX   << endl;//和上一个相同
    cout << endl;


    cout << "*****Minimum values:*****" << endl;
    cout << "int: "         << INT_MIN          << endl;
    cout << "short: "       << SHRT_MIN         << endl;
    cout << "long: "        << LONG_MIN         << endl;
    cout << "long long: "   << LLONG_MIN        << endl;
    cout << "long long: "   << LONG_LONG_MIN    << endl;//和上一个相同
    cout << "char: "        << CHAR_MIN         << endl;
    cout << "signed char: " << SCHAR_MIN        << endl;
    cout << endl;


    cout<<"Bits per byte = "<<CHAR_BIT<<endl;


    return 0;
}

64位win7运行结果

int is 4 bytes.
short is 2 bytes.
long is 4 bytes.
long long is 8 bytes.

*****Maximum values:*****
int: 2147483647
short: 32767
long: 2147483647
long long: 9223372036854775807
long long: 9223372036854775807
char: 127
signed char: 127
unsigned char: 127
unsigned short: 65535
unsigned long: 4294967295
unsigned long long: 18446744073709551615
unsigned long long: 18446744073709551615

*****Minimum values:*****
int: -2147483648
short: -32768
long: -2147483648
long long: -9223372036854775808
long long: -9223372036854775808
char: -128
signed char: -128

Bits per byte = 8




#include<iostream>
#include<string>
#include <limits>
using namespace std;

int main()
{
    cout << "type: \t\t" << "************size**************"<< endl;
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
    cout << "\t最大值:" << (numeric_limits<bool>::max)();
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
    cout << "char: \t\t" << "所占字节数:" << sizeof(char);
    cout << "\t最大值:" << (numeric_limits<char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
    cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
    cout << "\t最大值:" << (numeric_limits<signed char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
    cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: \t\t" << "所占字节数:" << sizeof(short);
    cout << "\t最大值:" << (numeric_limits<short>::max)();
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
    cout << "int: \t\t" << "所占字节数:" << 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;
    cout << "long: \t\t" << "所占字节数:" << 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;
    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;
    cout << "float: \t\t" << "所占字节数:" << 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: \t\t" << "************size**************"<< endl;
    return 0;
}

64位win7运行结果
type:           *****************************SIZE****************************
bool:           所占字节数:1   最大值:1               最小值:0
char:           所占字节数:1   最大值:               最小值:€
signed char:    所占字节数:1   最大值:               最小值:€
unsigned char:  所占字节数:1   最大值:              最小值:
wchar_t:        所占字节数:2   最大值:65535           最小值:0
short:          所占字节数:2   最大值:32767           最小值:-32768
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:    所占字节数:12  最大值:Infinity        最小值:0
float:          所占字节数:4   最大值:3.40282e+38     最小值:1.17549e-38
size_t:         所占字节数:4   最大值:4294967295      最小值:0
string:         所占字节数:4
type:           *****************************SIZE****************************

/*运行结果分析:

以上结果已经很明白了,一下补充说明几点:

概念、整型:表示整数、字符和布尔值的算术类型合称为整型(integral type)

关于带符号与无符号类型:整型 int、stort  和  long 都默认为带符号型。要获得无符号型则必须制定该类型为unsigned,比如unsigned long。unsigned int类型可以简写为unsigned,也就是说,unsigned后不加其他类型说明符就意味着是unsigned int。

一字节表示八位,即:1byte = 8 bit;

int: 4byte =  32 bit 有符号signed范围:2^31-1 ~ -2^31即:2147483647 ~ -2147483648无符号unsigned范围:2^32-1 ~ 0即:4294967295 ~ 0

long: 4 byte = 32 bit 同int型

double: 8 byte = 64 bit 范围:1.79769e+308 ~ 2.22507e-308

long double: 12 byte = 96 bit 范围: 1.18973e+4932 ~ 3.3621e-4932

float: 4 byte = 32 bit 范围: 3.40282e+038 ~ 1.17549e-038

int、unsigned、long、unsigned long 、double的数量级最大都只能表示为10亿,即它们表示十进制的位数不超过10个,即可以保存所有9位整数。而short只是能表示5位;


另外对于浮点说而言:使用double类型基本上不会有错。在float类型中隐式的精度损失是不能忽视的,二双精度计算的代价相对于单精度可以忽略。事实上,在有些机器上,double类型比float类型的计算要快得多。float型只能保证6位有效数字,而double型至少可以保证15位有效数字(小数点后的数位),long double型提供的精度通常没有必要,而且还要承担额外的运行代价。

double是8字节共64位,其中小数位占52位,2-^52=2.2204460492503130808472633361816e-16,量级为10^-16,故能够保证2^-15的所有精度。

在有些机器上,用long类型进行计算所付出的运行时代价远远高于用int类型进行同样计算的代价,所以算则类型前要先了解程序的细节并且比较long类型与int类型的实际运行时性能代价。



C++ 中,基本数据类型取值范围取决于其类型和所使用的系统架构(如 32 位或 64 位系统)。以下是常见的基本数据类型及其典型取值范围。 ### 整型(Integer Types) 整型用于存储不带小数部分的数值。C++ 提供了多种整型以适应不同的需求: - **`char`**:通常占用 1 字节,其取值范围为 -128 到 127(有符号)或 0 到 255(无符号)。 - **`short`**:通常占用 2 字节,其取值范围为 -32,768 到 32,767(有符号)或 0 到 65,535(无符号)。 - **`int`**:通常占用 4 字节,其取值范围为 -2,147,483,648 到 2,147,483,647(有符号)或 0 到 4,294,967,295(无符号)。 - **`long`**:通常占用 4 或 8 字节,具体取决于平台,取值范围与 `int` 类似或更大。 - **`long long`**:通常占用 8 字节,其取值范围为 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807(有符号)或 0 到 18,446,744,073,709,551,615(无符号)。 需要注意的是,整型变量即使被赋予浮点数,也只会保留其整数部分。例如,`int x = 3.75;` 会导致 `x` 的值为 `3`,而不是四舍五入的结果[^1]。 ### 浮点型(Floating-Point Types) 浮点型用于存储带有小数部分的数值: - **`float`**:单精度浮点数,通常占用 4 字节,其取值范围为大约 ±1.2e-38 到 ±3.4e38。 - **`double`**:双精度浮点数,通常占用 8 字节,其取值范围为大约 ±2.2e-308 到 ±1.8e308。 - **`long double`**:长双精度浮点数,通常占用 8 或 12 字节,其取值范围与 `double` 相似或更大,具体取决于平台[^2]。 ### 使用标准库查询取值范围 为了确保程序的可移植性,可以使用 C++ 标准库中的 `<limits>` 头文件来查询特定平台上各数据类型取值范围。例如: ```cpp #include <iostream> #include <limits> int main() { std::cout << "int 最大: " << std::numeric_limits<int>::max() << std::endl; std::cout << "int 最小值: " << std::numeric_limits<int>::min() << std::endl; std::cout << "double 最小值: " << std::numeric_limits<double>::min() << std::endl; std::cout << "double 最大: " << std::numeric_limits<double>::max() << std::endl; return 0; } ``` 这段代码将输出 `int` 和 `double` 类型的最大值和最小值,帮助开发者更好地理解当前平台上的数据类型特性[^3]。 ### 总结 C++ 中的基本数据类型提供了广泛的数值表示能力,从简单的字符到复杂的长双精度浮点数。了解这些类型的取值范围对于编写高效、可靠的程序至关重要。通过使用标准库提供的工具,可以轻松获取这些信息并确保程序在不同环境下的兼容性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值