Home >>C Tutorial >Type Casting in C
Typecasting in the C language generally permits the users to convert one data type into other data type. For the purpose of typecasting in the C language, Cast operators are used that is denoted by (type).
Here is the syntax of typecasting in the C language:
(type)value;
Point to be noted: Converting the lower value to higher in order to avoid data loss is always recommended in the C language.
Here are two examples for you to understand the use of typecasting in the C language:
1. The following example is without typecasting:
int f= 9/4; printf("f : %d\n", f );//Output: 2
2. The following example is with typecasting
float f=(float) 9/4; printf("f : %f\n", f );//Output: 2.250000
Here is an example of the typecasting in the C language for a better understanding:
In the following example, it is depicted that how typecasting cast the int value into the float value:
#include<stdio.h> int main() { float f= (float)15/4; printf("f : %f\n", f ); return 0; }
Here is another example of the typecasting:
#include<stdio.h> int main() { int x= (int)15/4; printf("x : %x\n", x ); return 0; }