Monday, February 23, 2009

Logical error on division

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.
Author : M.Kumaran

0 nhận xét:

Post a Comment