logical error on division

This tip submitted by M.Kumaran on 2005-08-17 02:32:10. It has been viewed 67293 times.
Rating of 5.6 with 606 votes



Look at the program

main()
{
   float ans;
   ans=5/10;
   printf("%f",ans);
}


what is the output?
The output is not like this: 0.50000

The output is 0.000000

because int/int = int
so change the program to
main()
{
   float ans;
   ans=5/10.0;
   printf("%f",ans);
}

the above programm is (int/float)
so the output is now 0.500000

int/int = int
int/float = float
float/int = float
float/float = float

The above table can be summarized as: integer division takes place only when both elements are integers.





More tips

Help your fellow programmers! Add a tip!