English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Type conversion allows us to convert one data type to another. In C language, we use the cast operator for type conversion, represented by (type).
Syntax:
(type)value;
Note: It is always recommended to convert from a lower value to a higher value to avoid data loss.
Type conversion example without using type conversion:
int f = 9/4; printf("f : %d\n", f);//Output: 2
Type conversion example:
float f = (float) 9/4; printf("f : %f\n", f );//Output: 2.250000
Let's see a simple example of converting an int value to a float.
#include<stdio.h> int main(){ float f= (float)9/4; printf("f : %f\n", f ); return 0; }
Output:
f : 2.250000