C++ Type Conversions

In this C++ Tutorial you will learn about type conversions in C++, what is a type conversion, how to achieve this, automatic conversion otherwise called as implicit conversion and type casting otherwise called as explicit conversion.

What is type conversion

It is the process of converting one type into another. In other words converting an expression of a given type into another is called type casting.

How to achieve this

There are two ways of achieving the type conversion namely:

  • Automatic Conversion otherwise called as Implicit Conversion
  • Type casting otherwise called as Explicit Conversion

Let us see each of these in detail:

Automatic Conversion otherwise called as Implicit Conversion

This is not done by any conversions or operators. In other words the value gets automatically converted to the specific type to which it is assigned.

Let us see this with an example:

  1.  #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. short x=6000;
  6. int y;
  7. y=x;
  8. }

In the above example the data type short namely variable x is converted to int and is assigned to the integer variable y.

So as above it is possible to convert short to int, int to float and so on.

Type casting otherwise called as Explicit Conversion

Explicit conversion can be done using type cast operator and the general syntax for doing this is :

datatype (expression);

Here in the above datatype is the type which the programmer wants the expression to gets changed as.

In C++ the type casting can be done in either of the two ways mentioned below namely:

  • C-style casting
  • C++-style casting

The C-style casting takes the synatx as

(type) expression

This can also be used in C++.

Apart from the above, the other form of type casting that can be used specifically in C++ programming language namely C++-style casting is as below namely:

type (expression)

This approach was adopted since it provided more clarity to the C++ programmers rather than the C-style casting.

Say for instance the as per C-style casting

(type) firstVariable * secondVariable

is not clear but when a programmer uses the C++ style casting it is much more clearer as below

type (firstVariable) * secondVariable

Let us see the concept of type casting in C++ with a small example:

  1.  #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. int a;
  6. float b,c;
  7. cout << "Enter the value of a:";
  8. cin >> a;
  9. cout << "Enter the value of b:";
  10. cin >> b;
  11. c = float(a)+b;
  12. cout << "The value of c is:" << c;
  13. }

The output of the above program is

In the above program a is declared as integer and b and c are declared as float. In the type conversion statement namely

  1. c = float(a)+b;

The variable a of type integer is converted into float type and so the value 10 is converted as 10.0 and then is added with the float variable b with value 12.5 giving a resultant float variable c with value as 22.5 .

[catlist id=165].

Related posts