(整理)c++中找出各種數據類型的取值范圍


今天去編程之美打醬油了,又遇到了數據類型取值范圍的問題。下面整理一下網上其他地方的資料。

整理自:

http://blog.csdn.net/xuexiacm/article/details/8122267

http://blog.csdn.net/yingevil/article/details/6690863 

如下:

#include<iostream>
#include<string>
#include <limits>
using namespace std;

int main()
{
    cout << "type: \t\t" << "************size**************" << endl;
    cout << "bool: \t\t" << "所占字節數:" << sizeof(bool);
    cout << "\t最大值:" << (numeric_limits<bool>::max)();
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
    cout << "char: \t\t" << "所占字節數:" << sizeof(char);
    cout << "\t最大值:" << (numeric_limits<char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
    cout << "signed char: \t" << "所占字節數:" << sizeof(signed char);
    cout << "\t最大值:" << (numeric_limits<signed char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
    cout << "unsigned char: \t" << "所占字節數:" << sizeof(unsigned char);
    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
    cout << "wchar_t: \t" << "所占字節數:" << sizeof(wchar_t);
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: \t\t" << "所占字節數:" << sizeof(short);
    cout << "\t最大值:" << (numeric_limits<short>::max)();
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
    cout << "int: \t\t" << "所占字節數:" << sizeof(int);
    cout << "\t最大值:" << (numeric_limits<int>::max)();
    cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
    cout << "unsigned: \t" << "所占字節數:" << sizeof(unsigned);
    cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
    cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
    cout << "long: \t\t" << "所占字節數:" << sizeof(long);
    cout << "\t最大值:" << (numeric_limits<long>::max)();
    cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
    cout << "long long: \t\t" << "所占字節數:" << sizeof(long long);
    cout << "\t最大值:" << (numeric_limits<long long>::max)();
    cout << "\t最小值:" << (numeric_limits<long long>::min)() << endl;
    cout << "unsigned long: \t" << "所占字節數:" << sizeof(unsigned long);
    cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
    cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
    cout << "double: \t" << "所占字節數:" << sizeof(double);
    cout << "\t最大值:" << (numeric_limits<double>::max)();
    cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
    cout << "long double: \t" << "所占字節數:" << sizeof(long double);
    cout << "\t最大值:" << (numeric_limits<long double>::max)();
    cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
    cout << "float: \t\t" << "所占字節數:" << sizeof(float);
    cout << "\t最大值:" << (numeric_limits<float>::max)();
    cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
    cout << "size_t: \t" << "所占字節數:" << sizeof(size_t);
    cout << "\t最大值:" << (numeric_limits<size_t>::max)();
    cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
    cout << "string: \t" << "所占字節數:" << sizeof(string) << endl;
    // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
    cout << "type: \t\t" << "************size**************" << endl;
    return 0;
}

代碼是參考這個的:

http://blog.csdn.net/xuexiacm/article/details/8122267

我這邊加入了long long類型的測試后運行結果是:

/*運行結果分析:

以上結果已經很明白了,一下補充說明幾點:

概念、整型:表示整數、字符和布爾值的算術類型合稱為整型(integral type)

關於帶符號與無符號類型:整型 int、stort  和  long 都默認為帶符號型。要獲得無符號型則必須制定該類型為unsigned,比如unsigned long。unsigned int類型可以簡寫為unsigned,也就是說,unsigned后不加其他類型說明符就意味着是unsigned int。

一字節表示八位,即:1byte = 8 bit;

int: 4byte =  32 bit有符號signed范圍:2^31-1 ~ -2^31即:2147483647 ~ -2147483648無符號unsigned范圍:2^32-1 ~ 0即:4294967295 ~ 0

long: 4 byte = 32 bit同int型

double: 8 byte = 64 bit范圍:1.79769e+308 ~ 2.22507e-308

long double: 12 byte = 96 bit范圍: 1.18973e+4932 ~ 3.3621e-4932

float: 4 byte = 32 bit范圍: 3.40282e+038 ~ 1.17549e-038

int、unsigned、long、unsigned long 、double的數量級最大都只能表示為10億,即它們表示十進制的位數不超過10個,即可以保存所有9位整數。而short只是能表示5位;

 

另外對於浮點說而言:使用double類型基本上不會有錯。在float類型中隱式的精度損失是不能忽視的,二雙精度計算的代價相對於單精度可以忽略。事實上,在有些機器上,double類型比float類型的計算要快得多。float型只能保證6位有效數字,而double型至少可以保證15位有效數字(小數點后的數位),long double型提供的精度通常沒有必要,而且還要承擔額外的運行代價。

double是8字節共64位,其中小數位占52位,2-^52=2.2204460492503130808472633361816e-16,量級為10^-16,故能夠保證2^-15的所有精度。

在有些機器上,用long類型進行計算所付出的運行時代價遠遠高於用int類型進行同樣計算的代價,所以算則類型前要先了解程序的細節並且比較long類型與int類型的實際運行時性能代價。

 

 

然后是另一篇關於__int64和long long的文章:

http://blog.csdn.net/yingevil/article/details/6690863 

也轉到這里:

                      C語言中long long的用法

  C語言的C99標准擴展了新的整數類型 long longlong32位寬,占4個字節,long long通常被定義成 64 位寬,也就可以實現了在32位機器上可以擴展8字節的數據,GUN C也支持,當然在64位平台上就存在這個問題了。C99標准並沒有硬性規定具體到某種平台上的某種整數類型究竟占用多少字節、能夠表示多大范圍的數值等,只是給出一條原則和一個參考數值集合,只要同時滿足這兩方面條件就算是符合 標准。 之后,我查看了C99標准:The rank of long long int shall be greater than the rank of long int,which shall be greater than the rank of int,which shall be greater than the rank of short int,which shall be greater than the rank of signed char.

意思是說: long long 的級別高於 long long 的級別高於 int int 的級別高於 short short 的級別高於 char 。(另外有 _Bool 永遠是最低級別)。級別高的整數類型的寬度大於等於級別較低的整數類型。

編譯long long需要支持C99標准的編譯器才行,VC並不支持,但有對應的類型__int64

                                  C++ __int64用法

  在做ACM題時,經常都會遇到一些比較大的整數。而常用的內置整數類型常常顯得太小了:其中long 和 int 范圍是[-2^31,2^31),即-2147483648~2147483647。而unsigned范圍是[0,2^32),即0~4294967295。也就是說,常規的32位整數只能夠處理40億以下的數。   那遇到比40億要大的數怎么辦呢?這時就要用到C++64位擴展了。不同的編譯器對64位整數的擴展有所不同。基於ACM的需要,下面僅介紹VC6.0g++編譯器的擴展。   VC64位整數分別叫做__int64unsigned __int64,其范圍分別是[-2^63, 2^63)[0,2^64),即-9223372036854775808~92233720368547758070~18446744073709551615(1800億億)。對64位整數的運算與32位整數基本相同,都支持四則運算與位運算等。當進行64位與32位的混合運算時,32位整數會被隱式轉換成64位整數。但是,VC的輸入輸出與__int64的兼容就不是很好了,如果你寫下這樣一段代碼:

那么,在第2行會收到error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '__int64' (or there is no acceptable conversion)的錯誤;在第3行會收到error C2593: 'operator <<' is ambiguous的錯誤。那是不是就不能進行輸入輸出呢?當然不是,你可以使用C的寫法:

scanf("%I64d",&a); printf("%I64d",a);

就可以正確輸入輸出了。當使用unsigned __int64時,把"I64d"改為"I64u"就可以了。   OJ通常使用g++編譯器。其64位擴展方式與VC有所不同,它們分別叫做long long 與 unsigned long long。處理規模與除輸入輸出外的使用方法同上。對於輸入輸出,它的擴展比VC好。既可以使用

cin>>a; 3 cout<<a;

也可以使用

scanf("%lld",&a); printf("%lld",a);
  最后我補充一點:作為一個特例,如果你使用的是Dev-C++g++編譯器,它使用的是"%I64d"而非"%lld"

總結:

 

輸入輸出long long 也可以借助printf,scanf語句,

但對應的占位符卻是和平台相關與編譯器相關的:

Linux中,gcc很統一的用%lld;在windows中,MinGWgccVC6都需要用%I64d

VS2008卻是用%lld

 

 

另外,以后做ACM之類的比賽,提交c++代碼時,一般是要選G++編譯器提交的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM