C/C++之最值limits.h(climits)和limits头文件


一、climits头文件

1.头文件

#include <limits.h>

或者

#include <climits>

2.符号常量

常用类型最大值的符号常量表示
CHAR_MAXchar 的最大值
SHRT_MAXshort 的最大值
INT_MAXint 的最大值
LONG_MAXlong 的最大值
LLONG_MAXlong long 的最大 值
常用类型最小值的符号常量表示
CHAR_MINchar 的最小值
SHRT_MINshort 的最小值
INT_MINint 的最小值
LONG_MINlong 的最小值
LLONG_MINlong long 的最小值
带符号与无符号的符号常量表示
SCHAR_MAXsinged char 的最大值
SCHAR_MINsigned char 的最小值
UCHAR_MAXunsigned char 的最大值
USHRT_MAXunsigned short 的最大值
UINT_MAXunsigned int 的最大值
ULONG_MAXunsigned 的最大值
ULLONG_MAXunsigned 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
  • 22
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值