Typecasting - Implicit and Explicit typecasting in C language

Type casting means converting one datatype to another datatype based upon the requirement. There are two types of type casting – Implicit type casting and Explicit type casting. Consider the following program.
Implicit typecasting: 
Before you execute, guess the output of the program shown below to differentiate what you know and what the compiler compiles.
#include<stdio.h>  
main()  
{  
 char ch;  
 int x=-5;  
 unsigned int y=5;  
 int a=5.5;  
 float b=5.5;  
 int c;  
 printf("%d\n",a);  
 c=a==b;  
 printf("c=%d\n",c);  
 c=x>y;  
 printf("c=%d\n",c);  
 ch='B';  
 c=ch>'A';  
 printf("c=%d\n",c);  
}  
We know that all real values are considered as double. In the declaration int a=5.5, 5.5 is a double type data, which is being assigned to an integer variable a. When we try print a using %d, you can see only the integral part of 5.5 i.e., 5. This is because the complier converts the double value 5.5 to integer as 5. This is an example of type casting; in specific it is called implicit type casting, as converting one datatype to another is take care by the compiler itself.
Now, consider the statement c=a==b. Here, = is least precedence when compared to ==. Hence comparison is done first and the result is assigned to c. In the comparison a==b, a is of integer datatype and b is of float type. The variable a is stored in normal integer format and that of b in IEEE format for floating point. In this comparison, the compiler converts the integer variable to float variable (implicit typecasting) and then compares them. Though b=5.5, when after typecasting, b becomes 5.0 instead of 5.5. Hence, compiler considers a==b as 5==5.0, which is, of course, unequal. This assigns 0 to c.
In the statement c=x>y, x>y is computed first and the result is assigned to c. Though we have not mentioned x as signed integer, int means signed int by default. So, x is signed and y is unsigned. The signed variable x is converted into unsigned data (implicit typecasting) and compared. Hence, the expression 5==5 results true and 1 is assigned to c.
In the statement c=ch>’A’, both ch and ‘A’ are of char datatype and are compared with each other. The result is true and 1 is assigned to c. There is no need of comparison in this statement. Here, remember that though ch and ‘A’ are characters, their numerical equivalents of ASCII values are compared.
Explicit typecasting:
Now, modify the respective statements in the above progarm as shown below.
c=a==(int)b;
c=x>(int)y;
In c=a==(int)b, a is an integer data; though b is float data, it is compared with a by “explicit” typecasting. The (int)b instructs the compiler to consider b as an integer data for comparison. Note that this explicit typecasting is not going to change the datatype of b forever, but for the instant it is instructed with different datatype for typecasting. Hence, this becomes true when 5==5 is compared.
In c=x>(int)y, x is signed integer; after explicit typecasting, the compiler gets the expression as -5>5, which is obviously false and 0 is assigned to c.
*In simple words, when the compiler takes care in operating with different datatypes together, it is called implicit typecasting. When the user forces to consider particular data as desired datatype, then it is called explicit typecasting.
Type casting from highest rank to lowest is shown below.
*Always, note that signed data is converted into unsigned data i.e., signed char is converted to unsigned char, signed short int is converted to unsigned short int etc.
For a still more understanding, execute the following program.
#include<stdio.h>  
main()  
{  
 int a=57;  
 printf("*%f*\t*%d*\t*%c*\n",(float)a,(int)a,(char)a);  
}  
Execute the following program and compare the output with expected results before you check the output. There are many ways to be wrong but only one way to be right.
#include<stdio.h>  
main()  
{  
 int a=57;  
 printf("*%hd*\t*%c*\t*%d*\n",(float)a,(int)a,(char)a);  
}  
Remember that type casting can also be performed upon pointers, which can be discussed in pointers.
Share:

0 comments:

Post a Comment