Type Conversion in C++

Type Conversion in C++:

Type conversion is needed when you assign a value of one data type to a variable of another data type.

Types of Type conversion in C++:

Implicit Type Casting (Type Promotion)

This type of casting is done automatically by the compiler. It is safe and do not cause data loss when conversion is from a smaller type to a larger type. For example:

  1. Converting a short to an int is safe because an int can represent all possible short values.
  2. Converting an int to a double is safe because a double can represent all possible int values without loss of precision.
  3. Converting a float to a double is safe because a double has a larger range and higher precision than a float.

Example:

#include 
using namespace std;
int main () {
   int c = 7;  
   float a = 3.142f;  
   double res = c * a;  
   cout << "Answer is: " << res;
   return 0; 
}  

  • In this case, when we multiply an int (c) by a float (a), implicit type conversion happens. Here, the integer c is implicitly promoted to a float. This is called type promotion. The multiplication is performed between these two floating-point precision variables and saved in a double-precision floating-point variable.

Output of the above program:

Answer is: 21.994

Explicit Type Casting:

This type of casting is done manually by the programmer. It is also only safe and does not cause data loss when conversion is from a smaller type to a larger type, as in implicit Type Casting. Then the question arises, why we need explicit type casting? The reasons are given below:

  1. C++ does not automatically determine all type conversions—some need to be explicitly handled to avoid data loss or unintended behavior. For example, conversions between incompatible pointer types or in polymorphic class hierarchies.
  2. Implicit type casting can sometimes lead to unintended or unsafe conversions. By using explicit casting, you can ensure that conversions are performed only when you explicitly request them.
  3. Explicit casting can help silence compiler warnings about potential data loss or type mismatches, as it makes your intentions explicit.
  4. Explicit casting makes it clear to anyone reading the code that a conversion is intentional, improving code readability and maintainability.

If you want to manually convert a type, you can do it easily in 2 ways.

C-style casting:

variable = (int)expression;
  • In this statement, the value of expression will be converted into an int and assigned to the variable.
  • However, if the original value contains a decimal (e.g., 4.19), the decimal part will be removed, and only 4 will be stored.

Let’s see an example:

#include 
using namespace std;
int main () {
    double d = 3415.3124;  
    int i = (int)d;
    cout <<"d= " <

The output of the above program is:

d= 3415.31
i= 3415

In this program, the value of the double variable d is stored in an int-type variable. The C-style cast (int)d truncates the decimal part of d, storing only the integer portion in i.

C++ Specific Type Casting:

But C-style type casting is not safe because it does not provide compile-time checks. It is better to use cast operators provided by C++ to fulfill the purpose of typecasting. They are also known as named casts. C++ provides four cast operators.

variable = static_cast (expression)
dynamic_cast (expression)	
const_cast(expression)	
reinterpret_cast(expression)

static_cast:

#include 
using namespace std;
int main () {
    
    double d = 3415.3124;  
    int i = static_cast(d);
    cout <<"d= " <

Output:

d= 3415.31
i= 3415

Explanation for dynamic cast, const_cast are not given here. Because they are of advanced level then this topic. To see their details. Please click here.

Risk associated with type conservation:

Type conversion (whether implicit or explicit) can lead to data loss, truncation, or even undefined behavior when converting from a larger type to a smaller one. For example, converting a double (e.g., 3.99) to an int will truncate the decimal part, resulting in 3. The value after conversion can also exceed the smaller type’s limit, causing overflow. For example, Converting long long to int. Another example is converting a very large floating-point number to an integer. It can cause overflow because the number could be too large for the integer type to store.

No Comments

Leave a Reply