fbpx

How Typecasting Works in C: Avoiding Data Loss and Overflow?

How Typecasting Works in C: Avoiding Data Loss and Overflow

INTRODUCTION

In C programming, managing data types effectively is key to writing efficient, accurate code. One of the fundamental ways to handle data conversion is through typecasting—the process where a programmer explicitly or implicitly converts one data type to another using a casting operator. Typecasting in C allows us to forcefully change data types, making it useful for optimizing code, but it comes with potential risks.

For example, when converting a floating-point number to an integer, as in float pi = 3.14; int integer_pi = pi;, part of the value is lost, reducing pi to just 3. This “narrowing conversion” can lead to data loss and precision issues, and similar issues arise when converting a large integer into a smaller type, like char or int.

Typecasting in C is the process where the programmer explicitly converts one data type to another using a casting operator. When typecasting, the target data type can be smaller than the source data type, which is why this kind of conversion is also known as a narrowing conversion.

Example:

Converting float into int

float pi = 3.14;

int integer_pi = pi;  // integer_pi becomes 3, losing .14

Converting from int to char:

  • When a large integer is stored in a char type, which typically has a smaller range, it can wrap around and result in unexpected values.

int x=5;

char ch=x;

Converting long to int:

    • Converting a long integer to an int when the long value exceeds the int range will lead to overflow.

long largeValue = 2147483648;  // Just above the int max limit

int intValue = largeValue;     // intValue may wrap around depending on the system

Types of Type Casting in C:

Types of Type Casting in C

Risks of Narrowing Conversions

Narrowing conversions can cause:

  • Data loss: Part of the value might be truncated, as in floating-point to integer conversions.
  • Overflow/Underflow: Values that exceed the target type’s limits can wrap around, leading to unexpected results.
  • Precision Loss: For example, converting a double to a float can reduce precision, as float has fewer significant digits.
  • Implicit type casting
  • Explicit type casting

Implicit type casting: (‘a’ is implicitly promoted to ‘int’)

 

short a = 5;

int b = 10;

int result = a + b; 

 

Float to Double Promotion:

When a function expects a double and a float is passed as an argument, the float is implicitly promoted to a double.

 

#include<stdio.h>

void  function(double d)

{

printf(“%lf”,d);

}

int main()

{

float f=9.22f;

function(f)

}

 

Converting int  to float:

 

int x=5;

float y=4.4;

float=x+y;

 

Converting char to int:

  • when a char is used in an arithmetic expression, it is implicitly converted to int (this is known as integer promotion).

char c=’a’

int ph=c+1

Explicit Typecasting:

Explicit typecasting is a converting  from one datatype to another datatype  explicity.

 

Without explicit conversion

—————————–

#include <stdio.h>

// Driver Code

int main()

{

    int a=10;

    int b=3;

    double d;

    d=a/b;

    printf(“%lf”,d);

            return 0;

}

Output :3.00000

 

With explictit  conversion:

 

//

// typecasting

#include <stdio.h>

// Driver Code

int main()

{

    int a=10;

    int b=3;

    double d;

    d=(double)a/b;

    printf(“%lf”,d);

                                 return 0;

}

The output is:3.5