C++ 學習筆記(一)數據類型

偶然發現很久以前剛學習c++的時候留下的筆記,發到網上保存一下。

C++數據類型

使用編程語言進行編程時,需要用到各種變量來存儲各種信息。變量保留的是它所存儲的值的內存位置。這意味着,當您創建一個變量時,就會在內存中保留一些空間。
您可能需要存儲各種數據類型(比如字符型、寬字符型、整型、浮點型、雙浮點型、布爾型等)的信息,操作系統會根據變量的數據類型,來分配內存和決定在保留內存中存儲什麼。


基本內置類型

下表列出了七種基本的 C++ 數據類型:

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

一些基本類型可以使用一個或多個類型修飾符進行修飾:signed、unsigned、short、long等。

下表爲各種變量類型佔用的內存,以及該類型的所能存儲的最值。

類型 範圍
char 1 個字節 -128 到 127 或者 0 到 255
unsigned char 1 個字節 0 到 255
signed char 1 個字節 -128 到 127
int 4 個字節 -2147483648 到 2147483647
unsigned int 4 個字節 0 到 4294967295
signed int 4 個字節 -2147483648 到 2147483647
short int 2 個字節 -32768 到 32767
unsigned short int 2 個字節 0 到 65,535
signed short int 2 個字節 -32768 到 32767
long int 8 個字節 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int 8 個字節 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int 8 個字節 0 to 18,446,744,073,709,551,615
float 4 個字節 +/- 3.4e +/- 38 (~7 個數字)
double 8 個字節 +/- 1.7e +/- 308 (~15 個數字)
long double 16 個字節 +/- 1.7e +/- 308 (~15 個數字)
wchar_t 2 或 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;
	cout << "type: \t\t" << "************size**************" << endl;
	return 0;
}

當上面的代碼被編譯和執行時,它會產生以下的結果:

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;
PS:本文內容並非原創,爲網絡蒐集,僅供學習交流使用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章