文章目录
一、climits头文件
1.头文件
#include <limits.h>
或者
#include <climits>
2.符号常量
常用类型最大值的符号常量 | 表示 |
---|---|
CHAR_MAX | char 的最大值 |
SHRT_MAX | short 的最大值 |
INT_MAX | int 的最大值 |
LONG_MAX | long 的最大值 |
LLONG_MAX | long long 的最大 值 |
常用类型最小值的符号常量 | 表示 |
---|---|
CHAR_MIN | char 的最小值 |
SHRT_MIN | short 的最小值 |
INT_MIN | int 的最小值 |
LONG_MIN | long 的最小值 |
LLONG_MIN | long long 的最小值 |
带符号与无符号的符号常量 | 表示 |
---|---|
SCHAR_MAX | singed char 的最大值 |
SCHAR_MIN | signed char 的最小值 |
UCHAR_MAX | unsigned char 的最大值 |
USHRT_MAX | unsigned short 的最大值 |
UINT_MAX | unsigned int 的最大值 |
ULONG_MAX | unsigned 的最大值 |
ULLONG_MAX | unsigned long 的最大值 |
- 常用类型的最小值就是最大值加1取负。
- 带符号与无符号:其中signed char的最大值与最小值是char的最大值与最小值一样,其他(无符号值)是最小值的绝对值与最大值的和。
3.示例
(1)常用类型的最大值
#include <iostream>
#include <climits> //limits.h
using namespace std;
int main() {
cout << "char: " << CHAR_MAX << endl;
cout << "short: " << SHRT_MAX << endl;
cout << "int: " << INT_MAX << endl;
cout << "long: " << LONG_MAX << endl;
cout << "long long: " << LLONG_MAX << endl;
/*
char: 127
short: 32767
int: 2147483647
long: 2147483647
long long: 9223372036854775807
*/
return 0;
}
(2)常用类型的最小值
#include <iostream>
#include <climits> //limits.h
using namespace std;
int main() {
cout << "char: " << CHAR_MIN << endl;
cout << "short: " << SHRT_MIN << endl;
cout << "int: " << INT_MIN << endl;
cout << "long: " << LONG_MIN << endl;
cout << "long long: " << LLONG_MIN << endl;
/*
char: -128
short: -32768
int: -2147483648
long: -2147483648
long long: -9223372036854775808
*/
return 0;
}
(3)带符号与无符号
#include <iostream>
#include <climits> //limits.h
using namespace std;
int main() {
cout << "signed char_max: " << SCHAR_MAX << endl;
cout << "signed char_min: " << SCHAR_MIN << endl;
cout << "unsigned char: " << UCHAR_MAX << endl;
cout << "unsigned short: " << USHRT_MAX << endl;
cout << "unsigned int: " << UINT_MAX << endl;
cout << "unsigned long: " << ULONG_MAX << endl;
cout << "unsigned long long: " << ULLONG_MAX << endl;
/*
signed char_max: 127
signed char_min: -128
unsigned char: 255
unsigned short: 65535
unsigned int: 4294967295
unsigned long: 4294967295
unsigned long long: 18446744073709551615
*/
return 0;
}
二、limits头文件
1.头文件
#include <limits>
2.格式
numeric_limits<T>::max()
numeric_limits<T>::min()
3.示例
(1)简单举一下常用的最大值
#include<iostream>
#include<limits>
using namespace std;
int main()
{
int k = numeric_limits<char>::max();
cout << k << endl;
//cout << numeric_limits<char>::max() << endl;这样写不知道为什么输出是空白
//cout << numeric_limits<short>::max()+0 << endl;这样写也有127的结果输出
cout << numeric_limits<short>::max() << endl;
cout << numeric_limits<int>::max() << endl;
cout << numeric_limits<long>::max() << endl;
cout << numeric_limits<long long>::max() << endl;
/*
32767
2147483647
2147483647
9223372036854775807
*/
cout << numeric_limits<float>::max() << endl;
cout << numeric_limits<double>::max() << endl;
/*
3.40282e+38
1.79769e+308
*/
return 0;
}
(2)简单举一下常用的最小值
#include<iostream>
#include<limits>
using namespace std;
int main()
{
int k = numeric_limits<char>::min();
cout << k << endl;
//cout << numeric_limits<char>::min() << endl;这样写不知道为什么输出是空白
//cout << numeric_limits<short>::min()+0 << endl;这样写也有127的结果输出
cout << numeric_limits<short>::min() << endl;
cout << numeric_limits<int>::min() << endl;
cout << numeric_limits<long>::min() << endl;
cout << numeric_limits<long long>::min() << endl;
/*
-128
-32768
-2147483648
-2147483648
-9223372036854775808
*/
cout << numeric_limits<float>::min() << endl;
cout << numeric_limits<double>::min() << endl;
/*
1.17549e-38
2.22507e-308
*/
return 0;
}
三、配合abs的陷阱
某类型最小负数的绝对值却没有对应此类型的正值,因为刚好小1。比如,int
的最小负数-2147483648
,而int
的最大正数是2147483647
。所以,理所应当的绝对值2147483647
却无法用int
表示出来。
而abs()
恰好陷入这个陷阱,顶多能扩充char和short得到正确的答案。
#include<iostream>
#include<limits>
using namespace std;
int main()
{
cout << abs(numeric_limits<char>::min()) << endl;
cout << abs(numeric_limits<short>::min()) << endl;
cout << abs(numeric_limits<int>::min()+0) << endl;
cout << abs(numeric_limits<long>::min()) << endl;
cout << abs(numeric_limits<long long>::min()) << endl;
/*
128
32768
-2147483648
-2147483648
-9223372036854775808
*/
return 0;
}
- 直接对本类型取反更不行:
short a = -32768;
a = -a;
cout << a << endl; // -32768
int b = -2147483648;
b = -b;
cout << b << endl; // -2147483648;
那么,如果不是绝对最小会出现,那么就取第二最小。
int a = numeric_limits<int>::min() + 1;
cout << abs(a) << endl; //2147483647