top
Loading...
C++ 數據類型

C++ 數據類型

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

您可能需要存儲各種數據類型(比如字符型、寬字符型、整型、浮點型、雙浮點型、布爾型等)的信息,操作系統會根據變量的數據類型,來分配內存和決定在保留內存中存儲什么。

基本的內置類型

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

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

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

  • 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 to 18,446,744,073,709,551,615
float4 個字節+/- 3.4e +/- 38 (~7 個數字)
double8 個字節+/- 1.7e +/- 308 (~15 個數字)
long double16 個字節+/- 1.7e +/- 308 (~15 個數字)
wchar_t2 或 4 個字節1 個寬字符

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

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

實例

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

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


北斗有巢氏 有巢氏北斗