C Type Casting

C Type Casting

In this chapter you will learn about type casting. Type casting allows you to convert data from one type to another type.

C 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 <stdio.h>      
#include <conio.h>    
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

				
			

Leave a Comment

Your email address will not be published. Required fields are marked *