C++ Data Types
When programming with a programming language, you need various variables to store various kinds of information. A variable holds the memory location of the value it stores. This means that when you create a variable, some space is reserved in memory.
You may need to store information of various data types (such as char, wchar_t, int, float, double, bool, etc.), and the operating system allocates memory and decides what to store in the reserved memory based on the variable's data type.
Basic Built-in Types
C++ provides programmers with a rich variety of built-in and user-defined data types. The table below lists the seven basic C++ data types:
| Type | The keyword |
|---|---|
| Boolean | bool |
| Character | char |
| Integer | int |
| Floating-point | float |
| Double | double |
| Void | void |
| Wide character |
wchar_t |
Actually, wchar_t comes from this:
typedef short int wchar_t;
So wchar_t actually occupies the same space as short int.
Some basic types can be modified using one or more type modifiers:
| Modifiers | Description | Example |
|---|---|---|
signed |
Indicates a signed type (default) | signed int x = -10; |
unsigned |
Indicates an unsigned type | unsigned int y = 10; |
short |
Indicates a short integer | short int z = 100; |
long |
Indicates a long integer | long int a = 100000; |
const |
Indicates a constant whose value cannot be modified | const int b = 5; |
volatile |
Indicates a variable that may be unexpectedly modified, forbidding compiler optimization | volatile int c = 10; |
mutable |
Indicates a class member can be modified in a const object. |
mutable int counter; |
The table below shows the memory required to store values for various variable types, as well as the maximum and minimum values such variables can hold.
**Note:** This varies across systems; one byte is 8 bits.
**Note:** By default, int, short, and long are all signed.
**Note:** long int is 8 bytes, int is 4 bytes; early C compilers defined long int as 4 bytes and int as 2 bytes, and the newer C/C++ standards remain compatible with this early setting.
| Data Type | Description | Size (bytes) | Range / Example values |
|---|---|---|---|
bool |
Boolean type, represents true or false | 1 | true or false |
char |
Character type, typically used to store ASCII characters | 1 | -128 to 127 or 0 to 255 |
signed char |
Signed char type | 1 | -128 to 127 |
unsigned char |
Unsigned char type | 1 | 0 to 255 |
wchar_t |
Wide character type, used to store Unicode characters | 2 or 4 | Depends on platform |
char16_t |
16-bit Unicode character type (introduced in C++11) | 2 | 0 to 65,535 |
char32_t |
32-bit Unicode character type (introduced in C++11) | 4 | 0 to 4,294,967,295 |
short |
Short integer | 2 | -32,768 to 32,767 |
unsigned short |
Unsigned short integer | 2 | 0 to 65,535 |
int |
Integer | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned int |
Unsigned integer | 4 | 0 to 4,294,967,295 |
long |
Long integer | 4 or 8 | Depends on platform |
unsigned long |
Unsigned long integer | 4 or 8 | Depends on platform |
long long |
Long long integer (introduced in C++11) | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
unsigned long long |
Unsigned long long integer (introduced in C++11) | 8 | 0 to 18,446,744,073,709,551,615 |
float |
Single-precision floating-point | 4 | Approximately ±3.4e±38 (6-7 significant digits) |
double |
Double-precision floating-point | 8 | Approximately ±1.7e±308 (15 significant digits) |
long double |
Extended-precision floating-point | 8, 12, or 16 | Depends on platform |
Types added in C++11
| Data Type | Description | Example |
|---|---|---|
auto |
Automatic type deduction | auto x = 10; |
decltype |
Obtain the type of an expression | decltype(x) y = 20; |
nullptr |
Null pointer constant | int* ptr = nullptr; |
std::initializer_list |
Initialization list type | std::initializer_list |
std::tuple |
Tuple type, can store multiple values of different types | std::tuple |
Note that the storage size of each type is related to the system's word size, but currently 64-bit systems are the most common.
The following lists the storage size differences between 32-bit and 64-bit systems (the same on Windows):
From the table above, we can see that the size of a variable varies depending on the compiler and the computer used.
The example below outputs the sizes of various data types on your computer.
Example
#include
<
iostream
#include
<
limits
using
namespace
std;
int
main
(
)
{
cout
<<
"
type:
\
t
\
t
"
<<
"
size**
"
<<
endl
;
cout
<<
"
bool:
\
t
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
bool
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
bool
::
max
)
(
)
;
cout
<<
"
\
t
\
t minimum value:
"
<<
(
numeric_limits
<
bool
::
min
)
(
)
<<
endl
;
cout
<<
"
char:
\
t
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
char
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
char
::
max
)
(
)
;
cout
<<
"
\
t
\
t minimum value:
"
<<
(
numeric_limits
<
char
::
min
)
(
)
<<
endl
;
cout
<<
"
signed char:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
signed
char
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
signed
char::
max
)
(
)
;
cout
<<
"
\
t
\
t minimum value:
"
<<
(
numeric_limits
<
signed
char::
min
)
(
)
<<
endl
;
cout
<<
"
unsigned char:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
unsigned
char
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
unsigned
char::
max
)
(
)
;
cout
<<
"
\
t
\
t minimum value:
"
<<
(
numeric_limits
<
unsigned
char::
min
)
(
)
<<
endl
;
cout
<<
"
wchar_t:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
wchar_t
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
wchar_t
::
max
)
(
)
;
cout
<<
"
\
t
\
t minimum value:
"
<<
(
numeric_limits
<
wchar_t
::
min
)
(
)
<<
endl
;
cout
<<
"
short:
\
t
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
short
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
short
::
max
)
(
)
;
cout
<<
"
\
t
\
t minimum value:
"
<<
(
numeric_limits
<
short
::
min
)
(
)
<<
endl
;
cout
<<
"
int:
\
t
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
int
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
int
::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
int
::
min
)
(
)
<<
endl
;
cout
<<
"
unsigned:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
unsigned
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
unsigned
::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
unsigned
::
min
)
(
)
<<
endl
;
cout
<<
"
long:
\
t
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
long
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
long
::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
long
::
min
)
(
)
<<
endl
;
cout
<<
"
unsigned long:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
unsigned
long
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
unsigned
long::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
unsigned
long::
min
)
(
)
<<
endl
;
cout
<<
"
double:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
double
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
double
::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
double
::
min
)
(
)
<<
endl
;
cout
<<
"
long double:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
long
double
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
long
double::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
long
double::
min
)
(
)
<<
endl
;
cout
<<
"
float:
\
t
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
float
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
float
::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
float
::
min
)
(
)
<<
endl
;
cout
<<
"
size_t:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
size_t
)
;
cout
<<
"
\
t maximum value:
"
<<
(
numeric_limits
<
size_t
::
max
)
(
)
;
cout
<<
"
\
t minimum value:
"
<<
(
numeric_limits
<
size_t
::
min
)
(
)
<<
endl
;
cout
<<
"
string:
\
t
"
<<
"
Bytes occupied:
"
<<
sizeof
(
string
)
<<
endl
;
// << "\tmax value: " << (numeric_limits
cout
<<
"
type:
\
t
\
t
"
<<
"
size**
"
<<
endl
;
return
0
;
}
This example uses endl, which inserts a newline after each line, << operator is used to pass multiple values to the screen, sizeof() the sizeof operator is used to obtain the size of various data types.
When the code above is compiled and executed, it produces the following result, which may differ depending on the computer used:
type: ************size**************
bool: 所占字节数:1 最大值:1 最小值:0
char: 所占字节数:1 最大值: 最小值:?
signed char: 所占字节数:1 最大值: 最小值:?
unsigned char: Size in bytes: 1 Maximum value: ? Minimum value:
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**************
Derived Data Types
| Data Type | Description | Example |
|---|---|---|
数组 |
A collection of elements of the same type | int arr[5] = {1, 2, 3, 4, 5}; |
指针 |
A type that stores the memory address of a variable | int* ptr = &x; |
引用 |
An alias for a variable | int& ref = x; |
函数 |
A function type, representing a function's signature | int func(int a, int b); |
结构体 |
A user-defined data type that can contain multiple members of different types | struct Point { int x; int y; }; |
类 |
A user-defined data type supporting encapsulation, inheritance, and polymorphism | class MyClass { ... }; |
联合体 |
Multiple members share the same block of memory | union Data { int i; float f; }; |
枚举 |
A user-defined set of integer constants | enum Color { RED, GREEN, BLUE }; |
Type Aliases
| Alias | Description | Example |
|---|---|---|
typedef |
Define an alias for an existing type | typedef int MyInt; |
using |
Define an alias for an existing type (introduced in C++11) | using MyInt = int; |
Standard Library Types
| Data Type | Description | Example |
|---|---|---|
std::string |
String type | std::string s = "Hello"; |
std::vector |
Dynamic array | std::vector |
std::array |
Fixed-size array (introduced in C++11) | std::array |
std::pair |
A container storing two values | std::pair |
std::map |
Key-value pair container | std::map |
std::set |
A collection of unique values | std::set |
typedef Declaration
You can use the typedef Gives a new name to an existing type. The syntax for defining a new type with typedef is as follows:
typedef type newname;
For example, the following statement tells the compiler that feet is another name for int:
typedef int feet;
Now, the following declaration is perfectly legal; it creates an integer variable distance:
feet distance;
Enumeration Type
An enumeration (enumeration) is a derived data type in C++; it is a set of enumeration constants defined by the user.
If a variable has only a few possible values, it can be defined as an enumeration type. "Enumeration" means listing the variable's values one by one; the variable's value can only be within the listed range of values.
To create an enumeration, you need to use the keyword enumenum. The general form of an enumeration type is:
enum 枚举名{
标识符[=整型常数],
标识符[=整型常数],
...
标识符[=整型常数]
} 枚举变量;
If the enumeration is not initialized, i.e., the "= integer constant" is omitted, it starts from the first identifier.
For example, the code below defines a color enumeration, and variable c is of type color. Finally, c is assigned the value "blue".
enum color { red, green, blue } c;
c = blue;
By default, the first name has value 0, the second has value 1, the third has value 2, and so on. However, you can also give a name a special value by simply adding an initializer. For example, in the enumeration below,green has the value 5.
enum color { red, green=5, blue };
Here,blue has the value 6, because by default each name is one greater than the preceding name, but the value of red remains 0.
Type Conversion
Type conversion is the process of converting the value of one data type into the value of another data type.
There are four types of casts in C++: static cast, dynamic cast, const cast, and reinterpret cast.
Static Cast
A static cast forcefully converts the value of one data type into the value of another data type.
Static cast is typically used for conversions between similar types, such as converting an int to a float.
Static cast performs no runtime type checking, so it may lead to runtime errors.
Example
int
i
=
10
;
float
f
=
static_cast
<
float
(
i
);
// Statically cast int to float
Dynamic Cast
Dynamic cast (dynamic_cast) is a mechanism in C++ used for downcasting within an inheritance hierarchy.
Dynamic cast is typically used to convert a base class pointer or reference into a derived class pointer or reference.
Dynamic cast performs type checking at runtime. If the conversion fails, it returns nullptr for pointer types, or throws a std::bad_cast exception for reference types.
Syntax:
dynamic_cast<目标类型>(表达式)
-
Target type: must be a pointer or reference type.
-
Expression: the base class pointer or reference to be converted.
Example: dynamic cast of pointer type
#include
<
iostream
class
Base
{
public:
virtual
~
Base
(
)
=
default
;
// The base class must have a virtual function
}
;
class
Derived
:
public
Base
{
public
:
void
show
(
)
{
std
::
cout
<<
"
Derived class method
"
<<
std
::
endl
;
}
}
;
int
main
(
)
{
Base
ptr_base
=
new
Derived
;
// Base class pointer points to a derived class object
// Convert base class pointer to derived class pointer
Derived
ptr_derived
=
dynamic_cast
<
Derived
*>
(
ptr_base
);
if
(
ptr_derived
)
{
ptr_derived
->
show
(
)
;
// Conversion succeeded, call derived class method
}
else
{
std
::
cout
<<
"
Dynamic cast failed!
"
<<
std
::
endl
;
}
delete
ptr_base
;
return
0
;
}
Output:
Derived class method
Example: dynamic cast of reference type
#include
#include
class
Base
{
public
:
virtual
~Base
(
)
=
default
;
// The base class must have a virtual function
}
;
class
Derived
:
public
Base
{
public
:
void
show
(
)
{
std
::
cout
<<
"Derived class method"
<<
std
::
endl
;
}
}
;
int
main
(
)
{
Derived derived_obj
;
Base
&
ref_base
=
derived_obj
;
// Base class reference bound to a derived class object
try
{
// Convert base class reference to derived class reference
Derived
&
ref_derived
=
dynamic_cast
<
Derived
&
(
ref_base
);
ref_derived.
show
(
)
;
// Conversion succeeded, call derived class method
}
catch
(
const
std
::
bad_cast
&
e
)
{
std
::
cout
<<
"Dynamic cast failed: "
<<
e.
what
(
)
<<
std
::
endl
;
}
return
0
;
}
Output:
Derived class method
| Feature | Pointer type | Reference type |
|---|---|---|
| Return value on failure | Return nullptr |
Throws std::bad_cast exception |
| Applicable scenarios | Downcasting, runtime type checking | Downcasting, runtime type checking |
| Performance overhead | Higher | Higher |
| Base class requirement | Must have a virtual function | Must have a virtual function |
Const Cast
Const cast is used to convert a const object into a non-const object.
Const cast can only remove the const attribute; it cannot change the object's type.
Example
const
int
i
=
10
;
int
&
r
=
const_cast
<
int
&>
(
i
);
// Const cast, convert const int to int
Reinterpret Cast
Reinterpret cast re-interprets the value of one data type as the value of another data type, typically used to convert between different data types.
Reinterpret cast performs no type checking, so it may lead to undefined behavior.
Example
int
i
=
10
;
float
f
=
reinterpret_cast
<
float
&>
(
i
);
// Reinterpret cast, convert int to float
