Type Casting and Type Conversion in C++ | Part-1

Type Casting and Type Conversion in C++ | Part-1
Type Casting and Type Conversion in C++ | Part-1

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 type casting 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++:

  1. 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.

// Program to Illustrate Implicit Conversion

#include<iostream>
using namespace std;
int main()
{
    float num = 4.5;
    char c = 'a';         //c implicitly converted to int. 
                            // ASCII value of 'a' is 97  & then to float => 97.0

    float sum=0.0;
    sum = num + c;
    cout<<"c is = "<<c<<endl; 
    cout<<"Sum is "<<sum<<endl;
    
    return 0;
}

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 the 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.

#include <iostream>
using namespace std;

int main() {

   int int_num;
   double double_num = 7.88;

   // implicit conversion
   // assigning a double value to an int variable

   int_num = double_num;
   
   cout<<"Integer number = "<<int_num<<endl;
   cout<<"Double number = "<<double_num<<endl;

   return 0;
}

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. 

  1. 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:

  1. C-style Type Casting
  2. Function style Casting
  3. Static_cast
  4. Const_cast
  5. Dynamic_cast
  6. 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:-

// Program to Illustrate C-style type casting
#include<iostream>
using namespace std;
int main(){
    int num;
    bool b = false;     // false =0 , true = 1 
    num = (int)b;      // c-style type casting
    cout<<"num is = "<<num<<endl;
    return 0;
}

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:

// Program to illustrate function-type casting

#include <iostream>
using namespace std;

int main()
{
    double num_double = 9.87;
    int num_int;
    num_int = int(num_double); // function-type casting
    cout<<"Integer number = "<<num_int;
    return 0;
}

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 that 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.

Frequently Asked Questions

What is type Casting, for example?

Type Casting is a manual conversion from one type to the desired type performed by the programmer. For example- int to float, char to int, long to double, long long to float, and so on.

Difference between the Implicit Conversion and Explicit Conversion in C++.

Implicit conversion is automatically performed by the compiler when differing data types are intermixed in an expression. Whereas, Explicit conversion is a user-defined conversion that forces an expression to be of a specific type.

How many types of casting operators are present in C++?

There are specific four type casting operators:-
static_cast
dynamic_cast
const_cast
reinterpret_cast

Key Takeaways

To summarise, we’ve discussed the overview on Type Casting and Type Conversion in C++. Type Casting is widely used in every programming language to modify the program, and it also assists in the debugging phase for better clarity. This is not it; the discussion is further divided into its part-2, where we have discussed the underlying type casting operators in detail. 

Don’t stop here Ninja, get yourself enrolled in our Top-notch courses.

If you found this article advantageous, share it with your friends. Stay tuned for more amazing content.

By: Alisha Chhabra