C - Data Type Type casting


You can cast from one type to another type.

For example you can cast from float to int type.

float debt;
int d = (int)debt 

In the preceding line, the float variable debt is typecast to an int value.

The int in parentheses directs the compiler to treat the value of debt as an integer.


#include <stdio.h> 

int main() //from ww  w . j a v a  2  s. co m
      int a,b; 
      float c; 

      printf("Input the first value: "); 
      printf("Input the second value: "); 
      c = a/b; 
      printf("%d/%d = %.2f\n",a,b,c); 

      c = (float)a/(float)b; 
      printf("%d/%d = %.2f\n",a,b,c); 



You type cast variables a and b in the equation, allowing the compiler to treat them as floating-point numbers.

Therefore, the result is what it should be.