Introduction
In general, the act of changing from one form to another is conversion. An example of conversion ( or transformation) is exchanging dollars for euros. In simple words, it’s converting the existing item with the newly desired item.
They are not converting anything because they are not aware of the concept of conversion, which is precisely our discussion today. Now, let’s get started with Type Casting and Type Conversion in C++.

Type Casting and Type Conversion in C++
In computer science, type conversion or typecasting refers to changing an entity of one data type into another. An example of typecasting is converting an integer to a string. This compares the two numbers when one is a string and the other an integer. Before moving ahead, let’s understand the difference between Type Casting and Type Conversion in C++.
In Type Casting, a datatype is converted into the desired data type by the programmer using the Casting operator. Whereas, in type conversion, the compiler itself converts the data type into the required data type.
There are two types of Type Conversion in C++:

Implicit Type Conversion
In Implicit ( or automatic ) type conversion, the compiler converts one data type to another data type. This process doesn’t need any user involvement. In simple words, the compiler is doing the conversion itself. It generally occurs when more than one data type is present in an expression.
Example 1:
int num= 45; float num2 = 7.8;
int sum = num + num2; // type of sum is an integer
What will be the value in the sum variable?
-
52
The compiler here automatically converts the floating number into the integer type, i.e., ( 7.8 to 7).
Example 2:
char a = ‘A’ ;
int num = 45;
int sum = a + num;
Now, Guess the output?
-
110
As we know, the ASCII value of A is 65. The compiler automatically adds the ASCII value(65) with num(45). Hence, the output comes as 110.
Type Promotion

Note:- It is possible to lose information in Implicit conversion as the signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur when a large datatype is transformed into a small byte of data type. ( e.g., long long is implicitly converted to float).
Now, let’s see the C++ example Codes to Illustrate the Implicit Conversion.
Output
c is = a
Sum is 101.5
Explanation:- In the above code, we’ve initialised two variables of type float and char.
Afterwards, the addition of both variables is being performed. Since the sum variable is of floating type, the c variable implicitly will get converted into the floating value. Here, the compiler itself changes one data type into the destination data type.
Let’s see an example in which we will be encountering Data Loss.
OUTPUT
Integer number = 7
Double number = 7.88
Since int cannot have a decimal part, the digits are truncated in the above example after the decimal point. Hence, Loss of data is possible in Implicit Conversion. You can also do a free certification of Basics of C++.
Explicit Conversion
When the user manually changes the data type of one to another, it is known as Explicit Conversion. This type of conversion is also known as Type Casting, where the user’s involvement is present. To understand the typecasting, we need to know about the cast operator. A cast operator is a unary operator which coerces one data type to be conveyed into another.
Further, Six types of Explicit Conversion are
- C-style Type Casting
- Function style Casting
- Static_cast
- Const_cast
- Dynamic_cast
- Reinterpret_cast

C-style Type Casting
As the name suggests, this type of casting is favoured by the C programming language. Cast notation is the other name for C-style casting.
Syntax:-
( new data_type) expression;
For example:
Output
num is = 0
In programming, 0 is false, and 1 is true. Since the b (bool) variable holds the false means (0), the num will be assigned with 0.
Now, if we already have Implicit Conversion, then why do we need Explicit Conversion?
Let’s understand with one example:
int a = 3; int b = 2;
float div = a/b;
Result = 1, which is imprecise.
When the explicit type casting is being performed, then the result will be precise.
float = (float) a/b;
Result = 1.5, which is accurate.
This precisely shows us the need for Explicit Conversions.
Function-style Casting
We can also use function notation to convert the data type into another type. It is like a function call where the type to be cast is the function’s name, and the value to be cast behaves like an argument to the function.
Syntax:
data_type ( expression );
Example:
Output
Integer number = 9
Explanation:- In the above code, we have declared the integer variable named num_int. Here, we assigned the value inside the num_double to the num_int variable. We can also perform the same process with the help of C-style Casting, which works exactly like the function style casting irrespective of the syntax.
Further, you can check out this blog’s part 2 on casting operators to read more about each one of them in detail.



