C++数值类型的取值范围

//C++数值类型的取值范围
#include "stdio.h"
#include "iostream"
using namespace std;
int main()
{

char a1=-128;//-128~+127
short b1=-32768;// -32768~+32767
int c1=-2147483648;//-2147483648~+2147483647
long d1=-2147483648;//-2147483648~+2147483647
long long x1=-9223372036854775808;//-9223372036854775808~9223372036854775807
//以下取-1转化后为最大值
unsigned char a2=-1;//0~255
unsigned short b2=-1;// 0~65535
unsigned int c2=-1;//0~4294967295
unsigned long d2=-1;//0~4294967295
unsigned long long x2=-1;//0~18446744073709551615
float e=100/3.0;//10e±38,6位有效数字-7
double f=100.0/3;//10e±308,12位有效数字-16
long double g=100.0/3;//10±4932,15位有效数字-16
cout.precision(50);
cout<<"测试平台:Intel P4,XPsp3-32bit,VC8.0"<<endl;
cout<<"浮点数精度测试100除以3"<<endl;
cout<<"float \t\t e="<<e<<endl;
cout<<"double \t\t f="<<f<<endl;
cout<<"long double \t g="<<g<<endl;
cout<<"有符号数取最小值分别是"<<endl;
printf("char a1   ->%d\n",a1);
cout<<"short b1 ="<<b1<<endl;
cout<<"int c1   ="<<c1<<endl;
cout<<"long d1   ="<<d1<<endl;
cout<<"long long x1 ="<<x1<<endl; 
cout<<"....最低值再减一,溢出得最大值"<<endl;
a1--;b1--;c1--;d1--;x1--;
printf("char a1   ->%d\n",a1);
cout<<"short b1 ="<<b1<<endl;
cout<<"int c1   ="<<c1<<endl;
cout<<"long d1   ="<<d1<<endl;
cout<<"long long x1 ="<<x1<<endl; 
cout<<"无符号数取最大值(-1)分别是"<<endl;
printf("unsigned char a2 ->%d\n",a2);
cout<<"unsigned short b2 ="<<b2<<endl;
cout<<"unsigned int c2   ="<<c2<<endl;
cout<<"unsigned long d2 ="<<d2<<endl;
cout<<"unsigned long long x2 ="<<x2<<endl;
cout<<"...最大值,加一后,溢出,归零(最小值)"<<endl;
a2++;b2++;c2++;d2++;x2++;
printf("unsigned char a2 ->%d\n",a2);
cout<<"unsigned short b2 ="<<b2<<endl;
cout<<"unsigned int c2   ="<<c2<<endl;
cout<<"unsigned long d2 ="<<d2<<endl;
cout<<"unsigned long long x2 ="<<x2<<endl;
cout<<"....零减一后,又溢出,得最大值,所以别拿无数号数和小于0比"<<endl;
a2--;b2--;c2--;d2--;x2--;
printf("unsigned char a2 ->%d\n",a2);
cout<<"unsigned short b2 ="<<b2<<endl;
cout<<"unsigned int c2   ="<<c2<<endl;
cout<<"unsigned long d2 ="<<d2<<endl;
cout<<"unsigned long long x2 ="<<x2<<endl;
cout<<"存储大小"<<endl;
cout<<"sizeof(char)   ="<<sizeof(char)<<"字节" <<endl;
cout<<"sizeof(short)   ="<<sizeof(short) <<"字节" <<endl;
cout<<"sizeof(int)   ="<<sizeof(int)<<"字节" <<endl;
cout<<"sizeof(long)   ="<<sizeof(long )<<"字节" <<endl;
cout<<"sizeof(long long) ="<<sizeof(long long)<<"字节" <<endl<<endl;
cout<<"sizeof(unsigned char)   ="<<sizeof(unsigned char)<<"字节" <<endl;
cout<<"sizeof(unsigned short)   ="<<sizeof(unsigned short )<<"字节" <<endl;
cout<<"sizeof(unsigned int)   ="<<sizeof(unsigned int )<<"字节" <<endl;
cout<<"sizeof(unsigned long)   ="<<sizeof(unsigned long )<<"字节" <<endl; 
cout<<"sizeof(unsigned long long) ="<<sizeof(unsigned long long)<<"字节" <<endl<<endl;
cout<<"sizeof(float)   ="<<sizeof(float )<<"字节" <<endl;
cout<<"sizeof(double)   ="<<sizeof(double)<<"字节" <<endl; 
cout<<"sizeof(long double) ="<<sizeof(long double )<<"字节" <<endl;
//其他方法#include "limits"//其他方法
/*
      cout<<"---------以下最大值--------------------"<<endl;   
      cout<<   numeric_limits<unsigned long long >::max()   <<endl;     
      cout<<   numeric_limits< long long >::max()   <<endl;     
      cout<<"---------以下最小值--------------------"<<endl;   
      cout<<   numeric_limits<unsigned long long >::min()   <<endl;     
      cout<<   numeric_limits<long long>::min()   <<endl;     
*/
//long long 取值-2E(sizeof(long long)*8)/2   ~   2E(sizeof(long long)*8)-1
//unsigned long long 取值0~2E(sizeof(unsigned long long)*8)-1
return 0;
}

 


测试结果:
浮点数精度测试100除以3
float            e=33.33333206176757800000000000000
double           f=33.33333333333333600000000000000
long double      g=33.33333333333333600000000000000
有符号数取最小值分别是
char a1   ->-128
short b1 =-32768
int c1   =-2147483648
long d1   =-2147483648
long long x1 =-9223372036854775808
....最低值再减一,溢出得最大值
char a1   ->127
short b1 =32767
int c1   =2147483647
long d1   =2147483647
long long x1 =9223372036854775807
无符号数取最大值(-1)分别是
unsigned char a2 ->255
unsigned short b2 =65535
unsigned int c2   =4294967295
unsigned long d2 =4294967295
unsigned long long x2 =18446744073709551615
...最大值,加一后,溢出,归零(最小值)
unsigned char a2 ->0
unsigned short b2 =0
unsigned int c2   =0
unsigned long d2 =0
unsigned long long x2 =0
....零减一后,又溢出,得最大值,所以别拿无数号数和小于0比
unsigned char a2 ->255
unsigned short b2 =65535
unsigned int c2   =4294967295
unsigned long d2 =4294967295
unsigned long long x2 =18446744073709551615
存储大小
sizeof(char)   =1字节
sizeof(short)   =2字节
sizeof(int)   =4字节
sizeof(long)   =4字节
sizeof(long long) =8字节


sizeof(unsigned char)   =1字节
sizeof(unsigned short)   =2字节
sizeof(unsigned int)   =4字节
sizeof(unsigned long)   =4字节
sizeof(unsigned long long) =8字节


sizeof(float)   =4字节
sizeof(double)   =8字节
sizeof(long double) =8字节


-

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值