TYPE CASTING IN C
~BY IGDTUW RESOURCE
Typecasting in C is the process of converting one data type to another data type by the programmer using the
casting operator during program design.
In typecasting, the destination data type may be smaller than the source data type when converting the data
type to another data type, that’s why it is also called narrowing conversion.
Syntax:
int x;
float y;
y = (float) x;
1. Implicit Type Casting
Implicit type casting in C is used to convert the data type of any variable without using the actual value that
the variable holds. It performs the conversions without altering any of the values which are stored in the data
variable. Conversion of lower data type to higher data type will occur automatically.
2. Explicit Type Casting
There are some cases where if the datatype remains unchanged, it can give incorrect output. In such cases,
typecasting can help to get the correct output and reduce the time of compilation. In explicit type casting, we
have to force the conversion between data types. This type of casting is explicitly defined within the program.
Advantages of Type Casting
1. Type casting in C programming makes the program very lightweight.
2. Type representation and hierarchies are some features we can take advantage of with the help of
typecasting.
3. Type casting helps programmers to convert one data type to another data type.
EXAMPLE:
#include <stdio.h>
// Driver Code
int main()
{
// Given a & b
int a = 15, b = 2;
float div;
// Division of a and b
div = a / b;
printf("The result is %f\n", div);
return 0;
}
Output:
The result is 7.000000