
- C++基礎
- C++主頁
- C++概述
- C++環境設定
- C++基本語法
- C++註釋
- C++ Hello World
- C++省略名稱空間
- C++常量/字面量
- C++關鍵字
- C++識別符號
- C++資料型別
- C++數值資料型別
- C++字元資料型別
- C++布林資料型別
- C++變數型別
- C++變數作用域
- C++多個變數
- C++基本輸入/輸出
- C++修飾符型別
- C++儲存類
- C++運算子
- C++數字
- C++列舉
- C++引用
- C++日期和時間
- C++控制語句
- C++決策制定
- C++ if 語句
- C++ if else 語句
- C++巢狀 if 語句
- C++ switch 語句
- C++巢狀 switch 語句
- C++迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++建構函式
- C++建構函式和解構函式
- C++複製建構函式
C++數值資料型別
C++中的數值資料型別用於處理數值資料,例如整數(有符號和無符號)、浮點值和精度值。
數值資料型別主要包含三種基本型別的數值資料,如下所示:
- int
- int
- short int
- long int
- long long int
- unsigned int
- unsigned short int
- unsigned long int
- unsigned long long int
- float
- double
- long double
在本文中,我們將詳細介紹所有數值資料型別及其子型別,並提供示例。
int 資料型別
**int** 資料型別是 integer(整數)的縮寫,它接受從 -231 到 (231-1) 的數值。它在記憶體中佔用 4 位元組(即 32 位)。
語法
int variable_name;
它進一步分為各種派生子型別,如下所示:
(a) short int
**short int** 用於較小的數字,因為它在記憶體中只佔用 2 位元組(即 16 位)。其值範圍為 -215 到 (215-1)。
語法
short int varianle_name;
(b) long int
**long int** 用於較大的數字,記憶體空間為 4 位元組(即 32 位),根據編譯器不同,最多可達 8 位元組(即 64 位)。
語法
long int variable_name;
(c) long long int
**long long int** 用於更大的數字,記憶體空間為 8 位元組(即 64 位),根據編譯器不同,最多可達 16 位元組(即 128 位)。其值範圍為 -263 到 (263-1)。
語法
long long int variable_name;
(d) unsigned int
**unsigned int** 用於儲存非負值,佔用與 **int** 資料型別相同的記憶體空間,即 4 位元組(即 32 位)。其值範圍為 0 到 (232-1)。
語法
unsigned int variable_name;
(e) unsigned short int
**unsigned short int** 用於儲存非負值,佔用與 short int 資料型別相同的記憶體空間,即 2 位元組(即 16 位)。其值範圍為 0 到 (216-1)。
語法
unsigned short int variable_name;
(f) unsigned long int
**unsigned long int** 用於儲存非負值,佔用與 **long int** 資料型別相同的記憶體空間,範圍為 4 位元組(即 32 位)到 8 位元組(即 64 位)。
語法
unsigned long int variable_name;
(g) unsigned long long int
**unsigned long long int** 用於儲存非負值,佔用與 **long long int** 資料型別相同的記憶體空間,範圍為 8 位元組(即 64 位)到 16 位元組(128 位)。其值範圍為 0 到 (264-1)。
語法
unsigned long long int variable_name;
int 資料型別示例
以下程式碼顯示了所有派生 int 資料型別的尺寸和用法:
#include <iostream> using namespace std; int main() { int a=16; short int b=3; long int c= -32; long long int d= 555; unsigned short int e=22; unsigned int f=33; unsigned long int g=888; unsigned long long int h=444444; cout << "sizeof int datatype is: " << sizeof(a) <<" and the number is: "<<a<< endl; cout << "sizeof short int datatype is: " << sizeof(unsigned int) <<" and the number is: "<<b<< endl; cout << "sizeof long int datatype is: " << sizeof(short int) <<" and the number is: "<<c<< endl; cout << "sizeof long long int datatype is: " << sizeof(unsigned short int) <<" and the number is: "<<d<< endl; cout << "sizeof unsigned short int datatype is: " << sizeof(long int) <<" and the number is: "<<e<< endl; cout << "sizeof unsigned int datatype is: " << sizeof(unsigned long int) <<" and the number is: "<<f<< endl; cout << "sizeof unsigned long int datatype is: " << sizeof(long long int) <<" and the number is: "<<g<< endl; cout << "sizeof unsigned long long int datatype is: " << sizeof(unsigned long long int) <<" and the number is: "<<h<< endl; return 0; }
輸出
sizeof int datatype is: 4 and the number is: 16 sizeof short int datatype is: 4 and the number is: 3 sizeof long int datatype is: 2 and the number is: -32 sizeof long long int datatype is: 2 and the number is: 555 sizeof unsigned short int datatype is: 8 and the number is: 22 sizeof unsigned int datatype is: 8 and the number is: 33 sizeof unsigned long int datatype is: 8 and the number is: 888 sizeof unsigned long long int datatype is: 8 and the number is: 444444
float 資料型別
**float** 資料型別用於浮點元素,即帶有小數部分的數字。此資料型別佔用 4 位元組(即 32 位)的記憶體。
語法
float elem_name;
float 資料型別示例
以下程式碼顯示了所有派生 int 資料型別的尺寸和用法:
#include <bits/stdc++.h> using namespace std; int main() { float k=1.120123; cout << "sizeof float datatype is: "<<sizeof(k)<<" and the element is: "<<k<<endl; return 0; }
輸出
sizeof float datatype is: 4 and the element is: 1.12012
double 資料型別
**double** 資料型別用於儲存與 **float** 相比具有雙精度浮點元素。此資料型別佔用 8 位元組(即 64 位)的記憶體。
語法
double elem_name;
double 資料型別還有進一步的分類,它佔用更多記憶體並存儲更精確的元素。
long double
從 double 派生的另一種資料型別是 long double,它佔用 16 位元組(即 128 位)的記憶體空間。
語法
double elem_name;
示例
以下程式碼顯示了所有派生 int 資料型別的尺寸和用法:
#include <bits/stdc++.h> using namespace std; int main() { double m=1.34000123; long double n=1.21312312421; cout << "sizeof double datatype is: "<<sizeof(m)<<" and the element is: "<<m<<endl; cout << "sizeof long double datatype is: "<<sizeof(n)<<" and the element is: "<<n<<endl; return 0; }
輸出
sizeof double datatype is: 8 and the element is: 1.34 sizeof long double datatype is: 16 and the element is: 1.21312
數值資料型別的顯式轉換
在 C++ 中,顯式型別轉換不會自動進行,需要手動將目標資料型別放在括號 () 中進行轉換。它告訴編譯器將變數或表示式的值轉換為特定(目標)資料型別。
示例
以下程式顯示了數值資料型別從一種型別到另一種型別的顯式轉換:
#include <bits/stdc++.h> using namespace std; int main() { double a=6.551555; cout<<"value of a (int) is "<<(int)a<<endl; cout<<"value of a (float) is "<<(float)a<<endl; cout<<"value of a (double) is "<<a<<endl; cout<<"Value of a (long double) is "<<(long double)a; return 0; }
輸出
value of a (int) is 6 value of a (float) is 6.55156 value of a (double) is 6.55155 Value of a (long double) is 6.55155