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字節


-
ios

相關文章
相關標籤/搜索