Type Conversion in C++3 min read

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

  1. Implicit Conversion
  2. 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:

Example of implicit type casting in C++ programming:

Output:


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.

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.

  1. Cast Notation (By assigning)
  2. 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:

Example:

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:

  • static_cast
  • dynamic_cast
  • const_cast
  • reinterpret_cast

However, these casting will be discussed later on this C++ tutorials.


MORE

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …