Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Sunday, September 29, 2024

Type Casting

 

Type Casting

In computer programming, type conversions involve changing the type of a variable from one data type to another. This can happen in two ways: implicit and explicit conversions. Along with this, terms like type promotion and type demotion describe how types are handled during conversions.

 

 1. Implicit Type Conversion (Type Coercion)

Implicit conversion happens automatically when the compiler converts a data type to another without the programmer’s intervention. This usually occurs when you mix data types in an operation, and the compiler needs to match them.

 

- Example in C++:

  int a = 10;

  double b = 5.5;

  double result = a + b; // 'a' is implicitly converted to double.

 

In this example, the integer `a` is automatically converted to a `double` because the expression involves a floating-point number (`b`). This type of conversion avoids data loss but may lead to larger data types.

 

 2. Explicit Type Conversion (Type Casting)

Explicit conversion happens when the programmer manually changes the data type of a variable. This is done using casting operators in languages like C++ or casting functions in other languages.

 

- Example in C++:

  double a = 9.7;

  int b = (int)a; // explicit conversion

 

Here, the floating-point value of `a` is explicitly cast to an integer, which will result in truncating the decimal part. Explicit conversions require caution since they may lead to data loss.

 

 3. Type Promotion

Type promotion occurs when smaller data types are automatically converted to larger data types during operations. This helps in avoiding data loss or overflows.

 

- Example:

  char c = 'A';   // char is promoted to int during arithmetic operation

  int result = c + 2;  // 'A' has ASCII value 65, promoted to int

 

In this example, the `char` type (`c`) is promoted to an `int` when it's used in an arithmetic operation. Type promotion ensures that operations are done with higher precision.

 

 4. Type Demotion

Type demotion occurs when larger data types are converted to smaller types, either implicitly or explicitly. This process can lead to data loss since the smaller data type might not be able to hold the full range of the original value.

 

- Example:

  double a = 100.99;

  int b = (int)a; // demotion of double to int, fractional part is lost

 

Here, the `double` value `100.99` is demoted to an `int`, causing the loss of the decimal part.

 

 Summary

- Implicit conversion: Happens automatically; involves promoting smaller types to larger ones.

- Explicit conversion: Programmer-enforced; useful when precision control is needed.

- Type promotion: Automatically converts smaller data types to larger ones.

- Type demotion: Reducing a larger data type to a smaller one, often resulting in data loss.

 

 

 

 

 

 

 

 

Program:

#include <iostream>

using namespace std;

int main() {

    // Implicit Type Conversion

    int a = 10;

    double b = 5.5;

    double implicit_result = a + b; // 'a' is implicitly converted to double

    cout << "Implicit Conversion (int to double): " << implicit_result << endl;

    // Explicit Type Conversion

    double c = 9.7;

    int d = (int)c; // explicit conversion from double to int

    cout << "Explicit Conversion (double to int): " << d << endl;

    // Type Promotion

    char e = 'A'; // 'A' has ASCII value 65

    int f = e + 2; // 'e' is promoted from char to int

    cout << "Type Promotion (char to int): " << f << " (ASCII of 'A' + 2)" << endl;

    // Type Demotion

    double g = 100.99;

    int h = (int)g; // demotion from double to int, fractional part is lost

    cout << "Type Demotion (double to int): " << h << endl;

    return 0;

}

No comments:

Post a Comment