English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn C through examples. ++Basic knowledge of type conversion.
C ++Allow us to convert data of one type to another type. This is called type conversion.
C ++There are two types of type conversions.
Implicit conversion
Explicit conversion (also known as type casting)
Related conversions: convert C ++String converted to int C ++ String converted to float / double
The type conversion done automatically by the compiler is called implicit type conversion. This type of conversion is also known asAutomatic conversion.
Let's look at two examples of implicit type conversion.
// How implicit type conversion works #include <iostream> using namespace std; int main() { // Assign an int value to num_int int num_int 9; // Declare a double variable double num_double // Implicit conversion // Assign an int value to a double variable num_double = num_int; cout << "num_int = " << num_int << endl; cout << "num_double = " << num_double << endl; return 0; }
Output result
num_int = 9 num_double = 9
In the program, we have assigned int data to a double variable.
num_double = num_int;
Here, before assigning an int value to the num_double variable, it will be automatically converted to double by the compiler. This is an example of implicit type conversion.
//How implicit type conversion works #include <iostream> using namespace std; int main() { int num_int; double num_double 9.99; // Implicit conversion // Assign a double value to an int variable num_int = num_double; cout << "num_int = " << num_int << endl; cout << "num_double = " << num_double << endl; return 0; }
Output result
num_int = 9 num_double = 9.99
In the program, we have assigned double data to an int variable.
num_double = num_int;
Here, before assigning the double value to the num_int variable, the compiler will automatically convert it to int. This is also an example of implicit type conversion.
Note:Since int cannot have a fractional part, the decimal part was truncated in the above example.
From the above examples, it can be seen that converting from one data type to another can easily lead to data loss. This happens when data of a larger type is converted to a smaller type.
When the user manually changes the data from one type to another, this is calledExplicit conversion. This type of conversion is also known asExplicit type conversion.
We can use three main methods in C ++Explicit conversion is used in the C type. They are:
C type type cast (also known as cast symbol)
Function notation (also known as old-style C++style type conversion)
Type Conversion Operators
As the name implies, this type of conversion isC programming language'sAlso known asExplicit conversion notation.
The syntax of this style is:
(data_type)expression;
For example,
// Initialization of int variable int num_int 26; // Declaration of double variable double num_double // Conversion from int to double num_double = (double)num_int;
We can also use functions with similar symbols to convert data from one type to another.
The syntax of this type is:
data_type(expression);
For example,
// Initialization of int variable int num_int 26; // Declaration of double variable double num_double // Conversion from int to double num_double = double(num_int);
#include <iostream> using namespace std; int main() { // Initialization of double variable double num_double 3.56; cout << "num_double = " << num_double << endl; // C style conversion from double to int int num_int1 = (int)num_double; cout << "num_int1 = " << num_int1 << endl; // Function style conversion from double to int int num_int2 = int(num_double); cout << "num_int2 = " << num_int2 << endl; return 0; }
Output result
num_double = 3.56 num_int1 = 3 num_int2 = 3
We useC-style type conversionsandFunction-style casts are used for type conversion,and display the result. Since they perform the same task, they both give us the same output.
In addition to these two type conversions, C ++There are also four operators for type conversion. They are calledType Conversion OperatorsThey are:
static_cast
dynamic_cast
const_cast
reinterpret_cast
We will learn these conversions in future tutorials.