各種類型網(wǎng)站建設(shè)獨(dú)立aso關(guān)鍵詞優(yōu)化計(jì)劃
目錄
一、基本的內(nèi)置類型
二、typedef聲明
三、枚舉類型
一、基本的內(nèi)置類型
C++ 為程序員提供了種類豐富的內(nèi)置數(shù)據(jù)類型和用戶自定義的數(shù)據(jù)類型。下表列出了七種基本的 C++ 數(shù)據(jù)類型:
類型 | 關(guān)鍵字 |
布爾型 | bool |
字符型 | char |
整型 | int |
浮點(diǎn)型 | float |
雙浮點(diǎn)型 | double |
無類型 | void |
寬字符型 | wchar_t |
其實(shí) wchar_t 是這樣來的:
?typedef short int wchar_t;
所以 wchar_t 實(shí)際上的空間是和 short int 一樣。
一些基本類型可以使用一個(gè)或多個(gè)類型修飾符進(jìn)行修飾:
- signed
- unsigned
- short
- long
下表顯示了各種變量類型在內(nèi)存中存儲(chǔ)值時(shí)需要占用的內(nèi)存,以及該類型的變量所能存儲(chǔ)的最大值和最小值。
注意:不同系統(tǒng)會(huì)有所差異,一字節(jié)為 8 位。
注意:默認(rèn)情況下,int、short、long都是帶符號(hào)的,即 signed。
注意:long int 8 個(gè)字節(jié),int 都是 4 個(gè)字節(jié),早期的 C 編譯器定義了 long int 占用 4 個(gè)字節(jié),int 占用 2 個(gè)字節(jié),新版的 C/C++ 標(biāo)準(zhǔn)兼容了早期的這一設(shè)定。
類型 | 位 | 范圍 |
char | 1 個(gè)字節(jié) | -128 到 127 或者 0 到 255 |
unsigned char | 1 個(gè)字節(jié) | 0 到 255 |
signed char | 1 個(gè)字節(jié) | -128 到 127 |
int | 4 個(gè)字節(jié) | -2147483648 到 2147483647 |
unsigned int | 4 個(gè)字節(jié) | 0 到 4294967295 |
signed int | 4 個(gè)字節(jié) | -2147483648 到 2147483647 |
short int | 2 個(gè)字節(jié) | -32768 到 32767 |
unsigned short int | 2 個(gè)字節(jié) | 0 到 65,535 |
signed short int | 2 個(gè)字節(jié) | -32768 到 32767 |
long int | 8 個(gè)字節(jié) | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
signed long int | 8 個(gè)字節(jié) | -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
unsigned long int | 8 個(gè)字節(jié) | 0 到 18,446,744,073,709,551,615 |
float | 4 個(gè)字節(jié) | 精度型占4個(gè)字節(jié)(32位)內(nèi)存空間,+/- 3.4e +/- 38 (~7 個(gè)數(shù)字) |
double | 8 個(gè)字節(jié) | 雙精度型占8 個(gè)字節(jié)(64位)內(nèi)存空間,+/- 1.7e +/- 308 (~15 個(gè)數(shù)字) |
long double | 16 個(gè)字節(jié) | 長(zhǎng)雙精度型 16 個(gè)字節(jié)(128位)內(nèi)存空間,可提供18-19位有效數(shù)字。 |
wchar_t | 2 或 4 個(gè)字節(jié) | 1 個(gè)寬字符 |
#include<iostream>
#include <limits>using namespace std; int main()
{ cout << "type: \t\t" << "************size**************"<< endl; cout << "bool: \t\t" << "所占字節(jié)數(shù):" << sizeof(bool); cout << "\t最大值:" << (numeric_limits<bool>::max)(); cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl; cout << "char: \t\t" << "所占字節(jié)數(shù):" << sizeof(char); cout << "\t最大值:" << (numeric_limits<char>::max)(); cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl; cout << "signed char: \t" << "所占字節(jié)數(shù):" << sizeof(signed char); cout << "\t最大值:" << (numeric_limits<signed char>::max)(); cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl; cout << "unsigned char: \t" << "所占字節(jié)數(shù):" << sizeof(unsigned char); cout << "\t最大值:" << (numeric_limits<unsigned char>::max)(); cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; cout << "wchar_t: \t" << "所占字節(jié)數(shù):" << sizeof(wchar_t); cout << "\t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \t\t" << "所占字節(jié)數(shù):" << sizeof(short); cout << "\t最大值:" << (numeric_limits<short>::max)(); cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl; cout << "int: \t\t" << "所占字節(jié)數(shù):" << sizeof(int); cout << "\t最大值:" << (numeric_limits<int>::max)(); cout << "\t最小值:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: \t" << "所占字節(jié)數(shù):" << sizeof(unsigned); cout << "\t最大值:" << (numeric_limits<unsigned>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: \t\t" << "所占字節(jié)數(shù):" << sizeof(long); cout << "\t最大值:" << (numeric_limits<long>::max)(); cout << "\t最小值:" << (numeric_limits<long>::min)() << endl; cout << "unsigned long: \t" << "所占字節(jié)數(shù):" << sizeof(unsigned long); cout << "\t最大值:" << (numeric_limits<unsigned long>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl; cout << "double: \t" << "所占字節(jié)數(shù):" << sizeof(double); cout << "\t最大值:" << (numeric_limits<double>::max)(); cout << "\t最小值:" << (numeric_limits<double>::min)() << endl; cout << "long double: \t" << "所占字節(jié)數(shù):" << sizeof(long double); cout << "\t最大值:" << (numeric_limits<long double>::max)(); cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl; cout << "float: \t\t" << "所占字節(jié)數(shù):" << sizeof(float); cout << "\t最大值:" << (numeric_limits<float>::max)(); cout << "\t最小值:" << (numeric_limits<float>::min)() << endl; cout << "size_t: \t" << "所占字節(jié)數(shù):" << sizeof(size_t); cout << "\t最大值:" << (numeric_limits<size_t>::max)(); cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl; cout << "string: \t" << "所占字節(jié)數(shù):" << sizeof(string) << endl; // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl; cout << "type: \t\t" << "************size**************"<< endl; return 0;
}
type: ? ? ? ? ************size**************
bool: ? ? ? ? 所占字節(jié)數(shù):1 ? ?最大值:1 ? ? ? ?最小值:0
char: ? ? ? ? 所占字節(jié)數(shù):1 ? ?最大值: ? ? ? ?最小值:?
signed char: ? ? 所占字節(jié)數(shù):1 ? ?最大值: ? ? ? ?最小值:?
unsigned char: ? ? 所占字節(jié)數(shù):1 ? ?最大值:? ? ? ? ?最小值:
wchar_t: ? ? 所占字節(jié)數(shù):4 ? ?最大值:2147483647 ? ? ? ?最小值:-2147483648
short: ? ? ? ? 所占字節(jié)數(shù):2 ? ?最大值:32767 ? ? ? ?最小值:-32768
int: ? ? ? ? 所占字節(jié)數(shù):4 ? ?最大值:2147483647 ? ?最小值:-2147483648
unsigned: ? ? 所占字節(jié)數(shù):4 ? ?最大值:4294967295 ? ?最小值:0
long: ? ? ? ? 所占字節(jié)數(shù):8 ? ?最大值:9223372036854775807 ? ?最小值:-9223372036854775808
unsigned long: ? ? 所占字節(jié)數(shù):8 ? ?最大值:18446744073709551615 ? ?最小值:0
double: ? ? 所占字節(jié)數(shù):8 ? ?最大值:1.79769e+308 ? ?最小值:2.22507e-308
long double: ? ? 所占字節(jié)數(shù):16 ? ?最大值:1.18973e+4932 ? ?最小值:3.3621e-4932
float: ? ? ? ? 所占字節(jié)數(shù):4 ? ?最大值:3.40282e+38 ? ?最小值:1.17549e-38
size_t: ? ? 所占字節(jié)數(shù):8 ? ?最大值:18446744073709551615 ? ?最小值:0
string: ? ? 所占字節(jié)數(shù):24
type: ? ? ? ? ************size**************
二、typedef聲明
可以使用 typedef 為一個(gè)已有的類型取一個(gè)新的名字。下面是使用 typedef 定義一個(gè)新類型的語(yǔ)法:
typedef type newname;?
?例如,下面的語(yǔ)句會(huì)告訴編譯器,feet 是 int 的另一個(gè)名稱:
typedef int feet;
三、枚舉類型
枚舉類型(enumeration)是C++中的一種派生數(shù)據(jù)類型,它是由用戶定義的若干枚舉常量的集合。
如果一個(gè)變量只有幾種可能的值,可以定義為枚舉(enumeration)類型。所謂"枚舉"是指將變量的值一一列舉出來,變量的值只能在列舉出來的值的范圍內(nèi)。
創(chuàng)建枚舉,需要使用關(guān)鍵字 enum。枚舉類型的一般形式為:
enum 枚舉名{?
? ? ?標(biāo)識(shí)符[=整型常數(shù)],?
? ? ?標(biāo)識(shí)符[=整型常數(shù)],?
...?
? ? 標(biāo)識(shí)符[=整型常數(shù)]
} 枚舉變量;