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

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More