Typecasting in C programming

Typecasting is a way to convert variables, constants or expression from one type to another type. Conversion from one type to another is often required in programming.

Consider a case where you want to find average of three numbers. Let us write a program to find average of three numbers.

#include <stdio.h>

int main()
{
    int num1, num2, num3;
    float average;
    num1 = 91;
    num2 = 85;
    num3 = 83;

    average = (num1 + num2 + num3) / 3;

    printf("Average = %f", average);

    return 0;
}

Average of 91, 85 and 83 is 86.33. But our program shows 86. What’s wrong going here? Is the C compiler gone mad? Why it is not showing exact average? I have also used float data type that is capable of storing real types.

It’s not the compilers fault. In the expression (num1 + num2 + num3) / 3, all variables and literals are integer type. Hence, integer division is performed instead of float division.

To ensure fractional division, we must typecast one of the operands to float. In the expression (float) (num1 + num2 + num3) / 3. Parenthesis and typecast operator has highest precedence. Hence, first sum of num1 + num2 + num3 is evaluated and converted to float type. Then after division is performed.

So to overcome the above integer division, we must typecast the expression to float type.

C supports two types of typecasting –

  1. Implicit typecasting
  2. Explicit typecasting

Implicit typecasting

Implicit typecast is automatic type conversion done by the compiler. Compiler automatically handles data type conversion. It converts variables and constants of lower type to higher type whenever required.

The automatic type conversion done by the compiler uses integer promotion rule.

Integer promotion

Compilers are smart at optimizing code for better performance. In the process of code optimization, the C compiler will perform integer promotion. The compiler automatically converts all operands to a common type (higher type used in expression). The process of converting a lower type to higher is known as integer promotion.

Integer promotion in C
Integer promotion in C

Example of implicit typecast

#include <stdio.h>
int main()
{
    char ch = 'A';
    int val = ch + 10; /* char ch is promoted to int before addition */
    
    printf("val = %d", val);

    return 0;
}

In the above program char is automatically converted to higher type int before performing addition.

Important note: Implicit conversion may result in data or sign loss. For example – when promoting long long to unsigned long long negative sign is lost. Also while promoting unsigned long long to float you may lose data.

Hence, it is often recommended to cast explicitly.

Explicit typecasting

Explicit typecasting is manual type conversion from one type to another type. In explicit cast we have full control over the conversion. Explicit conversion can be performed bi-directional i.e. you can cast both a lower type to higher as well as a higher type to lower.

Syntax of explicit typecast

(new-type) <variable-expression-literal>

Where new-type is a valid C data type.

Read more – List of all valid C primitive and derived data type

Example of explicit typecast

#include <stdio.h>

int main()
{
    int num1, num2, num3;
    float average;
    num1 = 91;
    num2 = 85;
    num3 = 83;

    average = (float)(num1 + num2 + num3) / 3;

    printf("Average = %f", average);

    return 0;
}

Important note: Explicit typecasting can also result in data loss. Conversion from float to int will lose decimal part.