在 C++ 中,<climits> 头文件定义了各种整型数据类型的极限值。这些极限值是特定于系统和编译器实现的宏常量,用于表示数据类型可以存储的最大和最小可能值
示例
以下是使用 <climits> 中定义的一些常量的示例:
#include <iostream>
#include <climits> // 或者 <limits.h>
using namespace std;
int main() {
cout << "char 最大值: " << CHAR_MAX << endl;
cout << "int 最小值: " << INT_MIN << endl;
cout << "unsigned long 最大值: " << ULONG_MAX << endl;
return 0;
}
顺便提一嘴->C++可以使用<string.h>或<cstring>的方式来使用字符串函数
而C语言一般就是用<string.h>
原本以为Limit只有限制的意思呢,以至于一开始看到这个陌生的头文件时不知ta是何物
在search了一下之后,oh,极限->想起了java的int最大值->Integer.MAX_VALUE
常用类型的极限值
-
INT_MAX : int 类型的最大值。
INT_MIN : int 类型的最小值。
UINT_MAX : unsigned int 类型的最大值。
LONG_MAX : long int 类型的最大值。
LONG_MIN : long int 类型的最小值。
LLONG_MAX : long long int 类型的最大值。
LLONG_MIN : long long int 类型的最小值。
#include <iostream>
#include <climits>
using namespace std;
int main()
{
//稍微来几个看一下怎么个事
cout << "The range of int is from " << INT_MIN << " to " << INT_MAX <<endl;
cout << "The maximum value of unsigned int is " << UINT_MAX <<endl;
cout << "The range of long long is from " << LLONG_MIN << " to " <<LLONG_MAX << std::endl;
return 0;
}
注意事项
使用 <climits> 时需要注意,某些类型的最小负数的绝对值可能没有对应的正数表示。例如,INT_MIN 的绝对值超出了 int 类型能表示的正数范围。因此,在使用这些极限值时应谨慎,以避免潜在的溢出问题->abs(INT_MIN)会报错。