C++ 數據類型

使用編程語言進行編程時,需要用到各種變數來存儲各種資訊。變數保留的是它所存儲的值的記憶體位置。這意味著,當您創建一個變數時,就會在內存中保留一些空間。

您可能需要存儲各種數據類型(比如字元型、寬字元型、整型、浮點型、雙浮點型、布爾型等)的資訊,操作系統會根據變數的數據類型,來分配記憶體和決定在保留記憶體中存儲什麼。

基本的內置類型

C++ 為程式員提供了種類豐富的內置數據類型和用戶自定義的數據類型。下表列出了七種基本的 C++ 數據類型:

類型關鍵字
布爾型bool
字元型char
整型int
浮點型float
雙浮點型double
無類型 void
寬字元型

wchar_t

其實 wchar_t 是這樣來的:

typedef short int wchar_t;

所以 wchar_t 實際上的空間是和 short int 一樣。

一些基本類型可以使用一個或多個類型修飾符進行修飾:

  • signed
  • unsigned
  • short
  • long

下表顯示了各種變數類型在內存中存儲值時需要佔用的記憶體,以及該類型的變數所能存儲的最大值和最小值。

注意:不同系統會有所差異。

類型範圍
char1 個位元組-128 到 127 或者 0 到 255
unsigned char1 個位元組0 到 255
signed char1 個位元組-128 到 127
int4 個位元組-2147483648 到 2147483647
unsigned int4 個位元組0 到 4294967295
signed int4 個位元組-2147483648 到 2147483647
short int2 個位元組-32768 到 32767
unsigned short int2 個位元組0 到 65,535
signed short int2 個位元組-32768 到 32767
long int8 個位元組-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 個位元組-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int8 個位元組0 到 18,446,744,073,709,551,615
float4 個位元組精度型占4個位元組(32位)記憶體空間,+/- 3.4e +/- 38 (~7 個數字)
double8 個位元組雙精度型占8 個位元組(64位)記憶體空間,+/- 1.7e +/- 308 (~15 個數字)
long double16 個位元組長雙精度型 16 個位元組(128位)記憶體空間,可提供18-19位有效數字。
wchar_t2 或 4 個位元組1 個寬字元

從上表可得知,變數的大小會根據編譯器和所使用的電腦而有所不同。

下麵實例會輸出您電腦上各種數據類型的大小。

實例

#include<iostream> 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; }

本實例使用了 endl,這將在每一行後插入一個換行符,<< 運算符用於向螢幕傳多個值。我們也使用 sizeof() 函數來獲取各種數據類型的大小。

當上面的代碼被編譯和執行時,它會產生以下的結果,結果會根據所使用的電腦而有所不同:

type:         ************size**************
bool:         所占位元組數:1    最大值:1        最小值:0
char:         所占位元組數:1    最大值:        最小值:?
signed char:     所占位元組數:1    最大值:        最小值:?
unsigned char:     所占位元組數:1    最大值:?        最小值:
wchar_t:     所占位元組數:4    最大值:2147483647        最小值:-2147483648
short:         所占位元組數:2    最大值:32767        最小值:-32768
int:         所占位元組數:4    最大值:2147483647    最小值:-2147483648
unsigned:     所占位元組數:4    最大值:4294967295    最小值:0
long:         所占位元組數:8    最大值:9223372036854775807    最小值:-9223372036854775808
unsigned long:     所占位元組數:8    最大值:18446744073709551615    最小值:0
double:     所占位元組數:8    最大值:1.79769e+308    最小值:2.22507e-308
long double:     所占位元組數:16    最大值:1.18973e+4932    最小值:3.3621e-4932
float:         所占位元組數:4    最大值:3.40282e+38    最小值:1.17549e-38
size_t:     所占位元組數:8    最大值:18446744073709551615    最小值:0
string:     所占位元組數:24
type:         ************size**************

typedef 聲明

您可以使用 typedef 為一個已有的類型取一個新的名字。下麵是使用 typedef 定義一個新類型的語法:

typedef type newname;

例如,下麵的語句會告訴編譯器,feet 是 int 的另一個名稱:

typedef int feet;

現在,下麵的聲明是完全合法的,它創建了一個整型變數 distance:

feet distance;

枚舉類型

枚舉類型(enumeration)是C++中的一種派生數據類型,它是由用戶定義的若干枚舉常量的集合。

如果一個變數只有幾種可能的值,可以定義為枚舉(enumeration)類型。所謂"枚舉"是指將變數的值一一列舉出來,變數的值只能在列舉出來的值的範圍內。

創建枚舉,需要使用關鍵字 enum。枚舉類型的一般形式為:

enum 枚舉名{
     識別字[=整型常數],
     識別字[=整型常數],
...
    識別字[=整型常數]
} 枚舉變數;

如果枚舉沒有初始化, 即省掉"=整型常數"時, 則從第一個識別字開始。

例如,下麵的代碼定義了一個顏色枚舉,變數 c 的類型為 color。最後,c 被賦值為 "blue"。

enum color { red, green, blue } c;
c = blue;

默認情況下,第一個名稱的值為 0,第二個名稱的值為 1,第三個名稱的值為 2,以此類推。但是,您也可以給名稱賦予一個特殊的值,只需要添加一個初始值即可。例如,在下面的枚舉中,green 的值為 5。

enum color { red, green=5, blue };

在這裏,blue 的值為 6,因為默認情況下,每個名稱都會比它前面一個名稱大 1,但 red 的值依然為 0。