Type Casting in C Programming

In this post we will talk about Data Type Casting in C Programming. Along with this, we will also understand the difference between implicit type casting and explicit type casting.

In our real life, we change (cast) many things from one type to another according to our needs like water into ice, milk into curd etc.

Similarly, in C programming, sometimes we have to change the value of one data type to the value of another data type and we call this process type casting.

In type casting, we mostly cast the value of one data type to the value of another data type in our programs and assign it to the variable of the changed type.

We also call type casting in C Programming as type conversion in C Programming. There are two types of type casting in C programming, Implicit and Explicit. Let us understand both the types one by one.

You can also read: Operator Precedence And Associativity In C Programming

Implicit Type Casting (Automatic Conversion)

As we read above, in type casting, we work with 2 types of data types, that is, we assign the value of one data type to the variable of another data type.

If both the data types are compatible with each other and the memory size of the data type to which the value is to be assigned is less than or equal to the data type to which the variable is to be assigned, then automatic type conversion is done by the compiler which we Also called implicit type casting.

The smallest to largest data type order is given in the image below and implicit type casting also works during assignment of this order.

Type Casting in C Programming

Type Casting in C Programming
Type Casting in C Programming

In most programming languages, when we do implicit type casting, then no data type is lost (truncated) from the value of the data type you are casting.

But data can be lost even in implicit conversions in C programming like when you cast signed data type to unsigned.

Apart from this, even when you cast the value of long data to float, the data may be lost due to overflow. Let us now understand implicit type casting with an example.

Type Casting in C Programming

Implicit Type Casting Example:

#include <stdio.h>
int main()
{
    short a=10;
    int b,x;
    char ch='A';
 
    //implicit type casting
    b=a;
    x=ch;
 
    printf("Value of A : %d\n",a);
    printf("Value of B : %d\n",b);
    printf("Value of X : %d\n",x);
 
    return 0;
}

Output:

Value of A : 10
Value of B : 10
Value of X : 65

Explanation:

Now as we have read in the C Data Types tutorial, the memory occupied size of short data type is less than the memory occupied size of int data type.

Therefore implicit type casting will be done by the compiler on Line 9 and no data will be lost either.

Similarly, on Line 10, when the value ‘A’ of char data type is assigned to the variable of int data type, then the compiler will automatically assign the ASCII value of ‘A’ 65 to int variable.

Explicit Type Casting (Narrowing Conversion)

Explicit type casting is performed by the programmer. The largest to smallest data type order is given in the image below and we also use explicit type casting in the assignment of the same order.

Type Casting in C Programming
Type Casting in C Programming

Let us now see the syntax of explicit type casting and then understand with examples why explicit type casting is needed in C programming.

Explicit Type Casting Syntax:

variable = (data_type) variable/value/expression;

We will assign the variable, value or expression to the variable by casting, but before that we have to write the data type in parenthesis ( ) to which type of variable we are assigning.

Explicit Type Casting Example Program 1:

#include<stdio.h>
int main()
{
    int n;
    float x;
 
    x=20.75;
 
    n=(int)x;
 
    printf("Value of X : %f\n",x);
    printf("Value of N : %d\n",n);
 
    return 0;
}

Output:

Value of X : 20.75
Value of N : 20

Note:

Data can be lost even in explicit conversions in C programming like when you cast float data type to int then decimal part will be lost.

Explicit Type Casting Example Program 2:

#include <stdio.h>
int main()
{
    int m1,m2,m3;
    float avg;
 
    m1=88;
    m2=93;
    m3=79;
 
    avg=(m1+m2+m3)/3;
 
    printf("Marks Average : %f",avg);
 
    return 0;
}

Output:

Marks Average : 86.00000

Explanation:

If you add the variables m1, m2 and m3 from your calculator and divide it by the number 3, then you will get 86.666664 output as an average.

But if you see the output of the example above, it is 86.000000 i.e. the decimal value after the point did not hold in the avg variable.

Because it does not matter which data type of variable you are assigning the value to. It makes a difference which data type is used before the assignment operator.

If you used explicit type casting on line 11, then the avg variable would have 86.666664 value assigned and output.

avg=(float)(m1+m2+m3)/3;

I hope that you have liked this post. If you liked the post, then please share it with your friends and relatives and you can send your suggestion in the comment box below or email me at pr***********@gm***.com.

You can also visit my Hindi YouTube Channel.

Leave a Comment