Type Conversion in C++: A Comprehensive Guide

In this topic, we will discuss data type conversion in the C++ programming language. Type conversion is the process of converting a variable’s predefined data type to an appropriate data type. The main purpose of type conversion is to convert variables of two different data types into a single data type so that mathematical and logical expressions can be easily resolved without data loss.

Type Conversion in C++: A Comprehensive Guide

For example, we are adding two numbers where one variable is of type int and the other is of type float; we need to convert the int variable to float so that both become float data types for addition.

In C++, type conversion can be done in two ways: implicit type conversion and explicit type conversion. The conversion performed automatically by the compiler is called implicit type conversion or automatic type conversion. The conversion performed by the user or requiring user intervention is called explicit type conversion. Let’s discuss implicit and explicit type conversion in C++.

Implicit Type Conversion

Implicit type conversion is the type conversion automatically performed by the compiler without any human intervention. This means that implicit conversion automatically converts one data type to another based on predefined rules of the C++ compiler. Therefore, it is also known as automatic type conversion.

For example:

int x = 20;short int y = 5;int z = x + y;

In the above example, there are two different data type variables x and y, where x is of type int and y is of type short int. The result variable z is also of integer type, which stores the values of x and y. However, before summing the two numbers, the C++ compiler automatically converts the lower rank data type (short int) value to a higher type (int). Thus, it avoids data loss, overflow, or sign loss in C++’s implicit type conversion.

Order of Implicit Type Conversion

Below is the correct order of data types from lower rank to higher rank:

bool -> char -> short int -> int -> unsigned int -> long int -> unsigned long int -> long long int -> float -> double -> long double
👇Click to Claim👇
👉C Language Knowledge Resource Collection

Program to Convert int to float Using Implicit Type Conversion

Let’s create a program that uses implicit type conversion to convert a lower rank data type to a higher rank type.

Program1.cpp

#include <iostream>using namespace std;int main (){   int num1 = 25;  // Assign integer value   float num2;     // Declare a float variable   num2 = num1;    // Use implicit conversion to convert int value to float variable   cout << " The value of num1 is: " << num1 << endl;   cout << " The value of num2 is: " << num2 << endl;   return 0;}

Output

The value of num1 is: 25The value of num2 is: 25

Program to Convert double to int Data Type Using Implicit Type Conversion

Let’s create a program that uses implicit type conversion to convert a higher data type to a lower type.

Program2.cpp

#include <iostream>using namespace std;int main(){   int num;                // Declare int type variable   double num2 = 15.25;    // Declare and assign value to double variable
   num = num2;             // Use implicit type conversion to assign double value to int variable   cout << " The value of the int variable is: " << num << endl;   cout << " The value of the double variable is: " << num2 << endl;   return 0;}

Output

The value of the int variable is: 15The value of the double variable is: 15.25

In the above program, we declared num as an integer type and num2 as a double data type variable, then assigned the value of num2 to num using the assignment operator. Therefore, the C++ compiler automatically converts the double data value to an integer type before assigning it to the integer type variable, printing the truncated value 15.

Explicit Type Conversion

The conversion that requires user intervention to convert a variable’s data type to another type is called explicit type conversion. In other words, explicit conversion allows programmers to manually convert data types from one type to another. Therefore, it is also known as type casting. Typically, we use explicit type conversion to force one type to be converted to another type, as it does not follow implicit conversion rules.

Explicit type conversion can be done in two ways:

  1. Using type casting

  2. Using assignment operator for explicit conversion

Program to Convert float Value to int Type Using Type Casting

Type casting: In C++, type casting is a unary operator that forces one type to be converted to another type.

Let’s consider an example where we use explicit type casting to convert float data type to int data type in C++.

Program3.cpp

#include <iostream>using namespace std;int main (){   float f2 = 6.7;   int x = static_cast<int>(f2);   // Use type casting operator to convert data from one type to another type   cout << " The value of x is: " << x;   return 0;}

Output

The value of x is: 6

Using Assignment Operator for Explicit Type Conversion

Let’s consider an example where we use the assignment operator in a C++ program to convert one variable’s data type to another variable’s data type.

Program4.cpp

#include <iostream>using namespace std;int main (){    float num2;             // Declare a float variable    int num1 = 25;          // Initialize an integer variable
    num2 = (float) num1;    // Use assignment operator to convert data from one type to another type    cout << " The value of int num1 is: " << num1 << endl;    cout << " The value of float num2 is: " << num2 << endl;    return 0;}

Output

The value of int num1 is: 25The value of float num2 is: 25.0

In the above program, we declared num2 as a float type variable and initialized num1 as an integer variable 25. Then using the assignment operator, we converted the data type of num1 from int to float and assigned it to num2. Thus, converting the integer type value 25 to float type and printing it as 25.0.

Type Conversion in C++: A Comprehensive Guide


 Popular Recommendations
  • CLion Tutorial – Naming Conventions in CLion

  • C Language Algorithm – “Path Sum” Algorithm Problem

  • C++ Tutorial – Range-based for loop in C++

Leave a Comment