C++ Tutorial C++ Advanced C++ Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ - Type Casting



The type casting is a method of converting the value of one data type to another data type. It is also known as type conversion. In C++, there are two kinds of conversions, which are given below:

  • Implicit Type Casting
  • Explicit Type Casting

Implicit Type Casting

The implicit type casting happens automatically when converting a smaller data types to larger data types. The compiler implicitly typecast the smaller data types to the larger data types. No data will be lost in this process.

C++ Implicit Casting

Example

The example below shows how the implicit type casting is done in C++.

#include <iostream>
using namespace std;
 
int main (){ 
  char num_char = 65;

  //implicit casting
  short num_short = num_char;
  int num_int = num_short;
  long num_long = num_int;
  float num_float = num_long;
  double num_double = num_float;
  long double num_long_double = num_double;

  //printing variables
  cout<<"num_char = "<<num_char<<"\n"; 
  cout<<"num_short = "<<num_short<<"\n";
  cout<<"num_int = "<<num_int<<"\n";
  cout<<"num_long = "<<num_long<<"\n";
  cout<<"num_float = "<<num_float<<"\n"; 
  cout<<"num_double = "<<num_double<<"\n"; 
  cout<<"num_long_double = "<<num_long_double<<"\n"; 
}

The output of the above code will be:

num_char = A
num_short = 65
num_int = 65
num_long = 65
num_float = 65
num_double = 65
num_long_double = 65

Explicit Type Casting

The explicit type casting does not happen automatically. It is performed manually by calling the compiler explicitly to typecast the larger data types into the smaller data types. There might be a data loss in this process.

C++ Explicit Casting

There are three major ways in which explicit conversion in C++ which are mentioned below:

  1. C-style type casting
  2. Function style type casting
  3. Type conversion operators

1. C-style type casting

This is also known as cast notation. The syntax of this method is given below:

(new_type)expression;

2. Function style type casting

The function style notation can also be used for type casting. The syntax of this style is given below:

new_type(expression);

Example

The example below shows how the perform explicit type casting using C-style and Function style type casting.

#include <iostream>
using namespace std;
 
int main (){    
  long double num_long_double = 68.75;

  //explicit casting - C style
  double num_double = (double) num_long_double;
  float num_float = (float) num_double;
  long num_long = (long) num_float;

  //explicit casting - Function style
  int num_int = int(num_long);
  short num_short = short(num_int);
  char num_char = char(num_short);
  bool num_bool = bool(num_char);

  //printing variables
  cout<<boolalpha;
  cout<<"num_long_double = "<<num_long_double<<"\n"; 
  cout<<"num_double = "<<num_double<<"\n"; 
  cout<<"num_float = "<<num_float<<"\n"; 
  cout<<"num_long = "<<num_long<<"\n"; 
  cout<<"num_int = "<<num_int<<"\n"; 
  cout<<"num_short = "<<num_short<<"\n"; 
  cout<<"num_char = "<<num_char<<"\n"; 
  cout<<"num_bool = "<<num_bool<<"\n"; 
}

The output of the above code will be:

num_long_double = 68.75
num_double = 68.75
num_float = 68.75
num_long = 68
num_int = 68
num_short = 68
num_char = D
num_bool = true

3. Type conversion operators

A Cast operator is an unary operator which forces one data type to be converted into another data type. C++ supports four types of casting:

  1. static_cast
  2. dynamic_cast
  3. const_cast
  4. reinterpret_cast

Example

The example below shows how to use type conversion operators.

#include <iostream>
using namespace std;
 
int main (){    
  float num_float = 100.55;

  //using static_cast operator
  int num_int = static_cast<int> (num_float);

  //printing variables
  cout<<"num_float = "<<num_float<<"\n"; 
  cout<<"num_int = "<<num_int<<"\n"; 
}

The output of the above code will be:

num_float = 100.55
num_int = 100 

5