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 数据类型是整数的缩写,其取值范围为 -231 至 (231-1)。它在内存中占用 4 个字节(即 32 位)。
语法
int variable_name;
它进一步分为各种派生子类型,如下所示:
(a) 短整型
短整型 用于较小的数字,因为它在内存中仅占用 2 个字节(即 16 位)。其值范围为 -215 到 (215-1)。
语法
short int variable_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 仅用于存储非负值,占用的内存空间与短整型数据类型相同,即 2 个字节(即 16 位)。其值范围为 0 到 (216-1)。
语法
unsigned short int variable_name;
(f) 无符号长整型
无符号长整型仅用于存储非负值,其占用的内存空间与长整型数据类型相同,范围从 4 字节(即 32 位)到 8 字节(即 64 位)。
语法
unsigned long int variable_name;
(g) unsigned long long int
unsigned long long int 仅用于存储非负值,其占用的内存空间与 long long int 数据类型相同,后者的大小范围从 8 字节(即 32 位)到 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 数据类型
与 float 相比,double 数据类型用于存储双精度浮点元素。此数据类型占用 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