C++常用資料類型

來源:互聯網
上載者:User

標籤:http   常用   class   float   c++   img   最大值   分享圖片   color   

 

基本的內建類型

C++ 為程式員提供了種類豐富的內建資料類型和使用者自訂的資料類型。下表列出了七種基本的 C++ 資料類型:

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

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

  • signed
  • unsigned
  • short
  • long

下表顯示了各種變數類型在記憶體中儲存值時需要佔用的記憶體,以及該類型的變數所能儲存的最大值和最小值。

注意:不同系統會有所差異。

 1 #include <iostream> 2  3 using namespace std; 4  5 int main() 6 { 7     //列印int類型的最大值 8     cout << "int類型最大值:" << INT_MAX << endl; 9     //列印int類型的最小值10     cout << "int類型最小值:" << INT_MIN << endl;11     //列印int所佔位元組數12     cout << "int類型位元組數:" << sizeof(int);13     return 0;14 }

 

類型 位元(位元組) 範圍
char 8位,1 個位元組 -128 到 127 或者 0 到 255
unsigned char 8位,1 個位元組 0 到 255
signed char 8位,1 個位元組 -128 到 127
int 32位,4 個位元組 -2147483648 到 2147483647
unsigned int 32位,4 個位元組 0 到 4294967295
signed int 32位,4 個位元組 -2147483648 到 2147483647
short int 16位,2 個位元組 -32768 到 32767
unsigned short int 16位,2 個位元組 0 到 65,535
signed short int 16位,2 個位元組 -32768 到 32767
long int 64位,8 個位元組 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int 64位,8 個位元組 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int 64位,8 個位元組 0 to 18,446,744,073,709,551,615
float 32位,4 個位元組 +/- 3.4e +/- 38 (~7 個數字)
double 32位,8 個位元組 +/- 1.7e +/- 308 (~15 個數字)
long double 128位,16 個位元組 +/- 1.7e +/- 308 (~15 個數字)
wchar_t 2 或 4 個位元組 1 個寬字元

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

 

C++常用資料類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.