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:


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:


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.


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 …
Read More

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More