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 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:
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long double
Example of implicit type casting in C++ programming:
using namespace std;
//integer type variable
int integer_num = 7;
//double type variable
//int assign to double, Implicit conversion
double_num = integer_num;
cout << "Integer Value: " << integer_num << endl;
cout << "Double Value: " << double_num << endl;
Integer Value: 7
Double Value: 7
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.
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.
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:
//c++ explicit type casting
using namespace std;
double num = 5.2;
int result = (int) num + 5;
cout << "Result: " << result;
Output: After execution following output will displayed.
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:
However, these casting will be discussed later on this C++ tutorials.
String Pattern Programs in C
Java Program to Find pair of Integers in Array whose sum is given Number
Program to Print Diamond Alphabet Patterns in C
Half Diamond Pattern in C using Alphabets
Half Pyramid of Alphabets in C