C++ 資料型別

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

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

注意:不同系統會有所差異,一位元組為 8 位。

注意:long int 8 個位元組,int 都是 4 個位元組,早期的 C 編譯器定義了 long int 佔用 4 個位元組,int 佔用 2 個位元組,新版的 C/C++ 標準相容了早期的這一設定。

型別範圍
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> #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; }

本例項使用了 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。