C++ programming allows us to convert the variables from one data type to another. A type cast basically means the conversion of one data type to another.
There are two types of conversion in C++:
- Implicit Conversion
- Explicit Conversion (or type casting)
Implicit Conversion
Implicit type conversion which is also known as ‘Automatic type conversion’, converts one data type to another without any external trigger from the user. The conversion is done by the compiler on its own that is why it is known as Automatic type.
This generally takes place in an expression where more than one data types are present, to avoid lose of data. In such conversion, variables with all data types are converted to the largest data type present. The precedence of conversion is in the following order:
1 2 3 4 5 | bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double |
Example of implicit type casting in C++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //implicit type-conversion #include <iostream> using namespace std; int main() { //integer type variable int integer_num = 7; //double type variable double double_num; //int assign to double, Implicit conversion double_num = integer_num; cout << "Integer Value: " << integer_num << endl; cout << "Double Value: " << double_num << endl; return 0; } |
Output:
1 2 | Integer Value: 7 Double Value: 7 |
Data Loss:
While Implicit conversion, data may get lost that is sign can be lost when signed type is implicitly converted to the unsigned type or when long is implicitly converted to float.
This happens when data of a larger type is converted to data of a smaller type.long double - > double -> float -> long -> short -> char
Example: Take an example shown below.
1 2 3 4 5 | int x; float y = 45.99; //assigning float value to an int variable x = y; |
As soon as the float variable is assign to the int variable, the compiler implicitly convert the float value to integer (with no decimal part).
Now if you print the value of x, the output will be 45. As you can see, the .99 (decimal part is lost). Since int
cannot have a decimal part, the digits after the decimal point are truncated in the above example.
Explicit Conversion
Sometimes, the situation may arise where the programmer might need to force the compiler to change the data type and that is when the Explicit casting is performed. When the user performs the data type conversion manually then it is called Explicit Casting.
This process is also called type casting and it is dependent on user.
Explicit conversion in C++ can be done in three different ways.
- Cast Notation (By assigning)
- Type conversion operators
1. Cast Notation (By assigning): It is a forceful method to convert the data types Explicitly. The required data type is defined explicitly in front of the expression in parenthesis.
Syntax for this style is:
1 | (data_type)expression; |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //c++ explicit type casting #include <iostream> using namespace std; int main() { double num = 5.2; //Explicit conversion int result = (int) num + 5; cout << "Result: " << result; return 0; } |
Output: After execution following output will displayed.
1 | Result: 10 |
2. Type conversion operators:
There are four types of casting operators in C++ which forces one data type to be converted into another data type. these are the unary data type. They are:
static_cast
dynamic_cast
const_cast
reinterpret_cast
However, these casting will be discussed later on this C++ tutorials.