C++程序设计语言练习4.5

C++标准库通过template  numeric_limits提供数值类型的极限值。使用这个模板,可以一定程度的提高程序的可移植性。

#include <iostream>
#include <limits>
using std::cout;
using std::endl;
using std::numeric_limits;
int main(){
  cout<<" the min of char is: "<<(int)numeric_limits<char>::min()<<endl;
  cout<<" the max of char is: "<<(int)numeric_limits<char>::max()<<endl;
  cout<<" the min of short is: "<<numeric_limits<short>::min()<<endl;
  cout<<" the max of short is: "<<numeric_limits<short>::max()<<endl;
  cout<<" the min of int is: "<<numeric_limits<int>::min()<<endl;
  cout<<" the max of int is: "<<numeric_limits<int>::max()<<endl;
  cout<<" the min of long is: "<<numeric_limits<long>::min()<<endl;
  cout<<" the max of long is: "<<numeric_limits<long>::max()<<endl;

  cout<<" the min of float is: "<<numeric_limits<float>::min()<<endl;
  cout<<" the max of float is: "<<numeric_limits<float>::max()<<endl;
  cout<<" the min of double is: "<<numeric_limits<double>::min()<<endl;
  cout<<" the max of double is: "<<numeric_limits<double>::max()<<endl;
  cout<<" the min of long double is: "<<numeric_limits<long double>::min()<<endl;
  cout<<" the max of long double is: "<<numeric_limits<long double>::max()<<endl;

  cout<<" the min of unsigned is: "<<numeric_limits<unsigned>::min()<<endl;
  cout<<" the max of unsigned is: "<<numeric_limits<unsigned>::max()<<endl;
}

运行结果:

[chaos@localhost cpp]$ g++ -o 4.5 ./4.5.cpp
[chaos@localhost cpp]$ ./4.5
 the min of char is: -128
 the max of char is: 127
 the min of short is: -32768
 the max of short is: 32767
 the min of int is: -2147483648
 the max of int is: 2147483647
 the min of long is: -9223372036854775808
 the max of long is: 9223372036854775807
 the min of float is: 1.17549e-38
 the max of float is: 3.40282e+38
 the min of double is: 2.22507e-308
 the max of double is: 1.79769e+308
 the min of long double is: 3.3621e-4932
 the max of long double is: 1.18973e+4932
 the min of unsigned is: 0
 the max of unsigned is: 4294967295




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值