Type Casting

Explicitly converts data types

 

(target data type)identifier

 

#include <stdio.h>
int main( )
{
	int myInt = 42 ;
	int yourInt = 17 ;
	int divInt = myInt / yourInt ;

	/*type cast myInt to a float data type*/
	float divFlo = (float)myInt / yourInt ;

	/*print the division without the type cast*/
	printf("%d \n", divInt) ;

	/*print the division with the type cast*/
	printf("%f \n", divFlo) ;

   return 0;
}

Compile & run:

2
2.470588

Leave a Reply