C Type Casting
In C language type casting or type conversion is a way to convert one data type into another. There are two types of type conversion. They are following:
- Implicit type conversion
- Explicit type conversion
Implicit type conversion
C automatically converts one type of data into another. This automatic conversion is known as implicit conversion. Such as if you put an integer value into a floating type variable the compiler will convert the integer value into float automatically. For example:
Example
int a = 53;
float b;
b = a;
Explicit type conversion
The way to force a type conversion it is called explicit conversion. The syntax of explicit conversion is following:
Syntax
(type_name) expression
Here type_name is one of the standard C data types. The expression may be a constant, variable or an expression. An example of explicit conversion is given below:
Example:
#include <stdio.h>
int main() {
float num;
num = (float) 27/7;
printf("%f", num);
return 0;
}
Output:
3.857143