整數利用三個關鍵字 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 | ||
long long | long long int | signed long long | signed long long int |
unsigned long long | unsigned long long int |
通常 short 與 int 至少是 16 位元, long 至少為 32 位元,而 long long 至少為 64 位元 ,視所用機器及編譯器而定。 signed 表示帶正負號, unsigned 表示不帶正負號,這是說 unsigned 的值為 0 或正整數。
long long 為 C++11 新增的資料型態。
以下程式計算出各種宣告組合在 64 位元機器所佔的位元組數
001 | #include <iostream> |
002 | |
003 | int main() { |
004 | std::cout << "short : " |
005 | << sizeof(short) |
006 | << std::endl; |
007 | std::cout << "short int : " |
008 | << sizeof(short int) |
009 | << std::endl; |
010 | std::cout << "signed short : " |
011 | << sizeof(signed short) |
012 | << std::endl; |
013 | std::cout << "signed short int : " |
014 | << sizeof(signed short int) |
015 | << std::endl; |
016 | std::cout << "unsigned short : " |
017 | << sizeof(unsigned short) |
018 | << std::endl; |
019 | std::cout << "unsigned short int : " |
020 | << sizeof(unsigned short int) |
021 | << std::endl; |
022 | std::cout << "int : " |
023 | << sizeof(int) |
024 | << std::endl; |
025 | std::cout << "signed : " |
026 | << sizeof(signed) |
027 | << std::endl; |
028 | std::cout << "signed int : " |
029 | << sizeof(signed int) |
030 | << std::endl; |
031 | std::cout << "unsigned : " |
032 | << sizeof(unsigned) |
033 | << std::endl; |
034 | std::cout << "unsigned int : " |
035 | << sizeof(unsigned int) |
036 | << std::endl; |
037 | std::cout << "long : " |
038 | << sizeof(long) |
039 | << std::endl; |
040 | std::cout << "long int : " |
041 | << sizeof(long int) |
042 | << std::endl; |
043 | std::cout << "signed long : " |
044 | << sizeof(signed long) |
045 | << std::endl; |
046 | std::cout << "signed long int : " |
047 | << sizeof(signed long int) |
048 | << std::endl; |
049 | std::cout << "unsigned long : " |
050 | << sizeof(unsigned long) |
051 | << std::endl; |
052 | std::cout << "unsigned long int : " |
053 | << sizeof(unsigned long int) |
054 | << std::endl; |
055 | std::cout << "long long : " |
056 | << sizeof(long long) |
057 | << std::endl; |
058 | std::cout << "long long int : " |
059 | << sizeof(long long int) |
060 | << std::endl; |
061 | std::cout << "signed long long : " |
062 | << sizeof(signed long long) |
063 | << std::endl; |
064 | std::cout << "signed long long int: " |
065 | << sizeof(signed long long int) |
066 | << std::endl; |
067 | std::cout << "unsigned long long : " |
068 | << sizeof(unsigned long long) |
069 | << std::endl; |
070 | std::cout << "unsigned long long int: " |
071 | << sizeof(unsigned long long int) |
072 | << std::endl; |
073 | |
074 | return 0; |
075 | } |
076 | |
077 | /* Kaiching Chang |
078 | u0303.cpp |
079 | 2014-02 */ |
編譯後執行,結果如下
$ g++ u0303.cpp |
$ ./a.out |
short : 2 |
short int : 2 |
signed short : 2 |
signed short int : 2 |
unsigned short : 2 |
unsigned short int : 2 |
int : 4 |
signed : 4 |
signed int : 4 |
unsigned : 4 |
unsigned int : 4 |
long : 8 |
long int : 8 |
signed long : 8 |
signed long int : 8 |
unsigned long : 8 |
unsigned long int : 8 |
long long : 8 |
long long int : 8 |
signed long long : 8 |
signed long long int: 8 |
unsigned long long : 8 |
unsigned long long int: 8 |
$ |
continue ...
沒有留言:
張貼留言