- 整數 (integer) 類
- 浮點數 (floating-point number) 類
整數類
布林值、字元、整數都屬於整數類的算術型態。布林值型態的變數使用關鍵字 (keyword) bool 宣告 (declare) ,代表真假值,不是 true 就是 false 。以下程式計算出 bool 所佔的位元組數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> int main() { std::cout << "bool : " << sizeof ( bool ) << std::endl; return 0; } /* 《程式語言教學誌》的範例程式 檔名:sizeofbool.cpp 功能:計算 bool 所佔的位元組數 作者:張凱慶 時間:西元 2010 年 10 月 */ |
編譯後執行,結果如下

字元型態有兩種,分別使用關鍵字 char 及 wchar_t 宣告,前者為機器的基本字元集,可與 signed 及unsigned 兩個修飾詞 (modifier) 搭配,後者則是機器的擴充字元集。以下程式計算出 char 及 wchar_t 所佔的位元組數 (byte)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> int main() { std::cout << "char : " << sizeof ( char ) << std::endl; std::cout << "signed char : " << sizeof ( signed char ) << std::endl; std::cout << "unsigned char : " << sizeof (unsigned char ) << std::endl; std::cout << "wchar_t : " << sizeof ( wchar_t ) << std::endl; return 0; } /* 《程式語言教學誌》的範例程式 檔名:sizeofchar.cpp 功能:計算 char 所佔的位元組數 作者:張凱慶 時間:西元 2010 年 10 月 */ |
編譯後執行,結果如下

整數利用三個關鍵字 short 、 int 及 long 進行宣告,另可與 signed 及unsigned 兩個修飾詞搭配,如下表
short | short int | signed short | signed short int |
unsigned short | unsigned short int | ||
int | signed | signed int | |
unsigned | unsigned int | ||
long | long int | signed long | signed long int |
unsigned long | unsigned long int |
通常 short 會是半個機器字長 (machine word) , int 為一個機器字長, long 可能會是一個或兩個,視所用機器及編譯器而定。 signed 表示帶正負號, unsigned 表示不帶正負號,這是說unsigned 的值為 0 或正整數。以下程式計算出各種宣告組合所佔的位元組數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> int main() { std::cout << "short : " << sizeof ( short ) << std::endl; std::cout << "short int : " << sizeof ( short int ) << std::endl; std::cout << "signed short : " << sizeof ( signed short ) << std::endl; std::cout << "signed short int : " << sizeof ( signed short int ) << std::endl; std::cout << "unsigned short : " << sizeof (unsigned short ) << std::endl; std::cout << "unsigned short int : " << sizeof (unsigned short int ) << std::endl; std::cout << "int : " << sizeof ( int ) << std::endl; std::cout << "signed : " << sizeof ( signed ) << std::endl; std::cout << "signed int : " << sizeof ( signed int ) << std::endl; std::cout << "unsigned : " << sizeof (unsigned) << std::endl; std::cout << "unsigned int : " << sizeof (unsigned int ) << std::endl; std::cout << "long : " << sizeof ( long ) << std::endl; std::cout << "long int : " << sizeof ( long int ) << std::endl; std::cout << "signed long : " << sizeof ( signed long ) << std::endl; std::cout << "signed long int : " << sizeof ( signed long int ) << std::endl; std::cout << "unsigned long : " << sizeof (unsigned long ) << std::endl; std::cout << "unsigned long int : " << sizeof (unsigned long int ) << std::endl; return 0; } /* 《程式語言教學誌》的範例程式 檔名:sizeofint.cpp 功能:計算 int 所佔的位元組數 作者:張凱慶 時間:西元 2010 年 10 月 */ |
編譯後執行,結果如下

浮點數類
浮點數資料型態,也就是帶有小數點的數字,使用關鍵字 float 及 double 宣告,另可用 long 修飾 double ,所以浮點數型態共有三種,如下列表
float |
double |
long double |
float 被稱為單精確度浮點數,有效數字到小數點後六位, double 稱為倍精確度浮點數,有效數字到小數點後十位,至於 long double 則稱為增廣精確度,有效數字視機器與編譯器的支援而定。以下程式計算出三者所佔的位元組數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> int main() { std::cout << "float : " << sizeof ( float ) << std::endl; std::cout << "double : " << sizeof ( double ) << std::endl; std::cout << "long double : " << sizeof ( long double ) << std::endl; return 0; } /* 《程式語言教學誌》的範例程式 檔名:sizeoffloat.cpp 功能:計算 double 所佔的位元組數 作者:張凱慶 時間:西元 2010 年 10 月 */ |
編譯後執行,結果如下

中英文術語對照 | |
---|---|
基本內建資料型態 | primitive built-in data type |
算術型態 | arithmetic type |
整數 | integer |
浮點數 | floating-point number |
關鍵字 | keyword |
宣告 | declare |
修飾詞 | modifier |
位元組數 | byte |
機器字長 | machine word |
您可以繼續參考
基本概念
標記
基本資料型態
相關目錄
回 C++ 快速導覽
回 C++ 教材
回首頁
參考資料
C++ reference
cplusplus.com
Cprogramming.com C++ Tutorial
C++ Primer, Fourth Edition, Stanley B. Lippman...
本文於 2013 年 1 月更新
沒有留言:
張貼留言