i have a question: printf("%f",9/5); it prints 0.000000 as output, shouldn't it be 1.000000. Or does it depends on the compiler?
This is a discussion on a problem within the C Programming forums, part of the General Programming Boards category; i have a question: printf("%f",9/5); it prints 0.000000 as output, shouldn't it be 1.000000. Or does it depends on the ...
i have a question: printf("%f",9/5); it prints 0.000000 as output, shouldn't it be 1.000000. Or does it depends on the compiler?
9/5 is an int
"%f" expects a double
int and double DO NOT MATCH!: you have Undefined Behaviour.
Try
orCode:printf("%d", 9/5);
Code:printf("%f", 9/5.0);
And also remeber the float can only give a certain amount of pricision as it takes just about machine word to store. If require better precision use double.
As a fundemental cause of the failure, when you divide two integers you will get an integer. To prodouce a floating result on the operand need to be target result type. As in this case float.
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein