Cplusplus 简明教程

C++ Numeric Data Types

C++ 中的数值数据类型用于处理数值数据,例如整数(带符号和不带符号)、浮点值和精度值。

Numeric data types in C++ are used to handle numerical data like integers (both signed and unsigned), floating-point values, and precision values.

数值数据类型主要包含三种基本类型:整数数据,如下所示−

Numeric data types mainly contain three fundamental types of numeric data, which are as follows −

  1. Int IntShort IntLong IntLong Long IntUnsigned IntUnsigned Short IntUnsigned Long IntUnsigned Long Long Int

  2. Float

  3. Double

  4. Long Double

在本文中,我们将详细介绍所有数值数据类型及其子类型,并附带示例。

In this article, we will go through all of the numeric data types and their subtypes in detail with examples.

int Data Type

int 数据类型是 integer 的缩写,它接受的数值范围为 -231 到 (231-1),在内存中占用 4 个字节(即 32 位)。

The int data type is short for integer, which takes numeric values from -231 to (231-1). It takes 4 Bytes (i.e., 32 bits) in the memory.

Syntax

int variable_name;

它进一步分类为各种派生子类型,如下所示 −

It is further classified into various derived subtypes, which are as follows −

(a) short int

short int 用于较小的数字,因为它在内存中仅占用 2 个字节(即 16 位)。它的值范围为 -215 到 (215-1)。

The short int is used for smaller numbers, as it takes only 2 Bytes (i.e., 16 bits) in the memory. It’s value ranges from -215to (215-1).

short int varianle_name;

(b) long int

long int 用于较大的数字,内存空间为 4 个字节(即 32 位),具体取决于编译器,最大可达 8 个字节(即 64 位)。

The long int is used for bigger numbers, with memory space of 4 bytes (i.e., 32 bits), and can be up to 8 bytes (i.e., 64 bits), depending upon the compiler.

long int variable_name;

(c) long long int

long long int 用于较大的数字,内存空间为 8 个字节(即 64 位),具体取决于编译器,最大可达 16 个字节(即 128 位)。它的值范围为 -263 到 (263-1)。

The long long int is used for larger numbers, with memory space of 8 bytes (i.e. 64 bits), and can be up to 16 bytes (i.e., 128 bits), depending upon the compiler. It’s value ranges from -263 to (263-1).

long long int variable_name;

(d) unsigned int

unsigned int 用于仅存储非负值,它占用的内存空间与 int 数据类型相同,即 4 个字节(即 32 位)。它的值范围为 0 到 (232-1)。

The unsigned int is used to store only non-negative value, and take up same memory space as an int data type, which is 4 Bytes (i.e. 32 bits). It’s value ranges from 0 to (232-1).

unsigned int variable_name;

(e) unsigned short int

unsigned short int 用于仅存储非负值,它占用的内存空间与 short int 数据类型相同,即 2 个字节(即 16 位)。它的值范围为 0 到 (216-1)。

The unsigned short int is used to store only non-negative value, and take up same memory space as a short int data type, which is 2 Bytes (i.e., 16 bits). It’s value ranges from 0 to (216-1).

unsigned short int variable_name;

(f) unsigned long int

unsigned long int 用于仅存储非负值,它占用的内存空间与 long int 数据类型相同,范围为 4 个字节(即 32 位)到 8 个字节(即 64 位)。

The unsigned long int is used to store only non-negative value, and take up same memory space as a long int data type, which ranges from 4 Bytes (i.e., 32 bits) to 8 Bytes (i.e., 64 bits).

unsigned long int variable_name;

(g) unsigned long long int

unsigned long long int 仅用于存储非负值,并且占用的内存空间与 long long int 数据类型相同,范围从 8 字节(即 32 位)到 16 字节(128 位)。其值范围为 0 到 (264-1)。

The unsigned long long int is used to store only non-negative value, and take up same memory space as a long long int data type, which ranges from 8 Bytes (i.e., 32 bits) to 16 Bytes (128 bits). It’s value ranges from 0 to (264-1).

unsigned long long int variable_name;

Example of int Data Type

下面代码展示了所有派生 int 数据类型的尺寸和用途——

The following code shows the size and use of all derived int data types −

#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 Data Type

float 数据类型用于浮点数元素,这些数字后面跟着一个点小数部分。此数据类型占用 4 字节(即 32 位)内存。

The float data type is used for floating-point elements, which are numbers that are followed by a decimal part. This data type takes 4 Bytes (i.e., 32 bits) of memory.

Syntax

float elem_name;

Example of float Data Type

下面代码展示了所有派生 int 数据类型的尺寸和用途——

The following code shows the size and use of all derived int data types −

#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 Data Type

double 数据类型用于存储与 float 相比较的双精度浮点数元素。此数据类型占用 8 字节(即 64 位)内存。

The double data type is used to store floating-point elements with double precision as compared to float. This data type takes 8 Bytes (i.e., 64 bits) of memory.

Syntax

double elem_name;

double 数据类型有进一步的分类,它占用更多内存并存储更精确的元素。

The double data type has a further categorization which takes up more memory and stores more precise elements.

long double

从 double 衍生的另一种数据类型是 long double,它占用 16 字节(即 128 位)的内存空间。

Another data type derived from double is long double, which takes 16 Bytes (i.e., 128 bits) of memory space.

double elem_name;

下面代码展示了所有派生 int 数据类型的尺寸和用途——

The following code shows the size and use of all derived int data types −

#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

Explicit Conversion of Numeric Data Types

在 C++ 中,不会自动进行明确类型转换,您需要通过在括号 () 中放置目标数据类型来手动转换类型。它告诉编译器将变量或表达式的值转换为特定(目标)数据类型。

In C++, the explicit type conversion is not done automatically, you need to convert the type manually by placing the target data type in the parentheses (). It tells the compiler to convert the value of a variable or an expression to a specific (target) data type.

Example

以下程序显示了从一个数字数据类型到另一个数字数据类型的显式转换 −

The following program shows the explicit conversion of numeric data types from one to another −

#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