In this chapter you will learn about type casting. Type casting allows you to convert data from one type to another type.
Table of Contents
ToggleC type casting
One data type can be converted to another data type through type casting. For type casting in C programming we use cast operator and it is expressed by (type).
C type casting syntax
(type)value;
Without type casting:
int f= 5/2;
printf("f : %d\n", f );//output: 2
Including type casting
float f=(float) 5/2;
printf("f : %f\n", f );//Output: 2.500000
Example: Use of type casting
Let’s see an example of casting an int value to a float:
#include
#include
int main(){
clrscr();
float f= (float)5/2;
printf("f : %f\n", f );
getch();
}
When the above program is compiled and executed it will output as follows:
f : 2.500000