5

Possible Duplicate:
Using %f to print an integer variable

#include<stdio.h>

int main()
{
    printf("%f",9/5);
    return 0;   
}

Output: 0.000000

Can anyone explain the output of the above program ?

Shouldn't be the output of program be 1.000000 ?

Community
  • 1
  • 1
dark_shadow
  • 3,503
  • 11
  • 56
  • 81
  • 1
    Enable your compiler warnings, you should get informed that `printf` argument types don't match the format string. – Kos Jul 28 '12 at 17:43
  • Also if you want the output `1.000000` then use `(float)(9/5)`, seems that no answer mentioned that for some reason – Kos Jul 28 '12 at 17:45
  • @pb2q: It's not really a duplicate of that question. This one has two issues: `int` division yielding `int`, and passing the result to `printf` with `"%f"`. I'm not sure I've seen a question with that combination. – Keith Thompson Jul 28 '12 at 17:47
  • 2
    The OP is still aware of the int division, though. It does add an unnecessary layer if it's not thought to be part of the problem, yes. – chris Jul 28 '12 at 17:49
  • 1
    @Kos: gcc gives auch warnings; do other compilers? And you might as well cast to `double`, since it will be promoted anyway. – Keith Thompson Jul 28 '12 at 17:50
  • @chris: A good point (that I just came back here to mention). Voting to close as duplicate. – Keith Thompson Jul 28 '12 at 17:55

6 Answers6

5
printf("%f",9/5);

This statement is undefined behavior because the argument has to be of type double and 9 / 5 is of type int.

Use 9 / 5.0 to have an argument of type double (and the correct floating division).

ouah
  • 142,963
  • 15
  • 272
  • 331
4

9/5 is of type int. Passing an int argument to printf with "%f" has undefined behavior.

Try 9.0/5.0 if you want 1.800000, or (double)(9/5) if you want 1.000000

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
3

Why is this?

So 9 / 5 is obtained by dividing two integers -- and dividing two integers results in an integer value, 1. But, since it's an int, and the %f specified wants a float, this is undefined behavior.

Ps.: you probably want

9.0 / 5

or

9 / 5.0

or

9.0 / 5.0
2

Re: Shouldn't be the output of program be 1.000000 ?

It would have been had your division resulted in a float value rather than an int. Since you tried to print an int with a %f format specification, the result was undefined behavior.

More details:

There are two problems, related though:

  1. You are getting an integer result from your division, but trying format for a float value using %f

  2. (Your question seems to imply you are aware of this) Integer division results in an integer which is why the %f format specifier isn't appropriate. When the format specifier and the argument don't match the behavior is undefined.

    To avoid an integer result, try:

    9.0 / 5 or 9 / 5.0 or even 9.0 / 5.0

    to get the result you were expecting and then the %f specifier will be appropriate and work as expected.

    Note that when you are doing integer division, the result will be another integer. If, however, at least one of the operands is a float, the other operand will be promoted to a float too and the division will result in a float value.

    With integer division you get truncation, which means the fractional part of the result is thrown away.

Levon
  • 138,105
  • 33
  • 200
  • 191
  • The output should be 1.0 if this would be an example of integer division, but the output is 0.0. That's because the %f format expects a double and he provides an int. – Femaref Jul 28 '12 at 17:41
  • @Femaref Agreed, I was adjusting my answer as you were making your comment :) .. thanks. – Levon Jul 28 '12 at 17:42
0

That is because 9/5 is 1 and is type of integer. The fraction part is simply truncated. To achieve what you want, force the division to make floating point operations with 5.0/9 or 5/9.0 or 5.0/9.0 or (float) 5/9

When type of integer argument is bound with %f, then the behaviour is undefined.

This is stated in C99 standard Section 7.21.6.1 Paragraph 9. This paragraph is quoted below:

If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

phoxis
  • 60,131
  • 14
  • 81
  • 117
  • 1
    No. The output is 0.0, not 1.0. That's because he is trying to display an int with %f as the format. – Femaref Jul 28 '12 at 17:40
0

Answer is simple. 9 is integer and 5 is also integer. when integer will be divided by integer then the result will be 0 and it will be escalated to 0.00000 because you have used "%f" in printf() If you want the correct result perform 9.0/5 Now you will for sure get the correct answer

Daarks Solutions
  • 81
  • 1
  • 1
  • 2
  • How does this add anything new to the answers posted 20 minutes ago? – Levon Jul 28 '12 at 18:00
  • 1
    @Levon The answer is simple: Because the typing in SO is faster than the speed of light, new users can't follow this and obviously they answer BEFORE they realize that someone else has already answered ;) – pankar Jul 28 '12 at 18:17