1. 程式人生 > >C、C++資料型別(int, long, short, char, long long) 的取值範圍、最大最小值:climits 裡的一些巨集

C、C++資料型別(int, long, short, char, long long) 的取值範圍、最大最小值:climits 裡的一些巨集

#include <iostream>
#include <climits>

using namespace std;

int main()
{

    cout << "int is "       <<sizeof(int)       << " bytes." << endl;
    cout << "short is "     <<sizeof(short)     << " bytes." << endl;
    cout << "long is "      <<sizeof(long)      << " bytes." << endl;
    cout << "long long is " <<sizeof(long long) << " bytes." << endl;
    cout << endl;


    cout << "*****Maximum values:*****" << endl;
    cout << "int: "                 << INT_MAX          << endl;
    cout << "short: "               << SHRT_MAX         << endl;
    cout << "long: "                << LONG_MAX         << endl;
    cout << "long long: "           << LLONG_MAX        << endl;
    cout << "long long: "           << LONG_LONG_MAX    << endl;//和上一個相同
    cout << "char: "                << CHAR_MAX         << endl;
    cout << "signed char: "         << SCHAR_MAX        << endl;
    cout << "unsigned char: "       << CHAR_MAX         << endl;
    cout << "unsigned short: "      << USHRT_MAX        << endl;
//    cout << "unsigned int: "        << UNIT_MAX         << endl;//報錯
    cout << "unsigned long: "       << ULONG_MAX        << endl;
    cout << "unsigned long long: "  << ULLONG_MAX       << endl;
    cout << "unsigned long long: "  << ULONG_LONG_MAX   << endl;//和上一個相同
    cout << endl;


    cout << "*****Minimum values:*****" << endl;
    cout << "int: "         << INT_MIN          << endl;
    cout << "short: "       << SHRT_MIN         << endl;
    cout << "long: "        << LONG_MIN         << endl;
    cout << "long long: "   << LLONG_MIN        << endl;
    cout << "long long: "   << LONG_LONG_MIN    << endl;//和上一個相同
    cout << "char: "        << CHAR_MIN         << endl;
    cout << "signed char: " << SCHAR_MIN        << endl;
    cout << endl;


    cout<<"Bits per byte = "<<CHAR_BIT<<endl;


    return 0;
}
64位win7執行結果

int is 4 bytes.
short is 2 bytes.
long is 4 bytes.
long long is 8 bytes.

*****Maximum values:*****
int: 2147483647
short: 32767
long: 2147483647
long long: 9223372036854775807
long long: 9223372036854775807
char: 127
signed char: 127
unsigned char: 127
unsigned short: 65535
unsigned long: 4294967295
unsigned long long: 18446744073709551615
unsigned long long: 18446744073709551615

*****Minimum values:*****
int: -2147483648
short: -32768
long: -2147483648
long long: -9223372036854775808
long long: -9223372036854775808
char: -128
signed char: -128

Bits per byte = 8
#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 << "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;
}
64位win7執行結果
type:           *****************************SIZE****************************
bool:           所佔位元組數:1   最大值:1               最小值:0
char:           所佔位元組數:1   最大值:               最小值:€
signed char:    所佔位元組數:1   最大值:               最小值:€
unsigned char:  所佔位元組數:1   最大值:              最小值:
wchar_t:        所佔位元組數:2   最大值:65535           最小值:0
short:          所佔位元組數:2   最大值:32767           最小值:-32768
int:            所佔位元組數:4   最大值:2147483647      最小值:-2147483648
unsigned:       所佔位元組數:4   最大值:4294967295      最小值:0
long:           所佔位元組數:4   最大值:2147483647      最小值:-2147483648
unsigned long:  所佔位元組數:4   最大值:4294967295      最小值:0
double:         所佔位元組數:8   最大值:1.79769e+308    最小值:2.22507e-308
long double:    所佔位元組數:12  最大值:Infinity        最小值:0
float:          所佔位元組數:4   最大值:3.40282e+38     最小值:1.17549e-38
size_t:         所佔位元組數:4   最大值:4294967295      最小值:0
string:         所佔位元組數:4
type:           *****************************SIZE****************************

/*執行結果分析:

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

概念、整型:表示整數、字元和布林值的算術型別合稱為整型(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型別的實際執行時效能代價。