2

I would like to know why the output in this program is 0.0000:

{
    int a = 65;
    char c = (char)a;
    int m = 3.0/2;
    printf("%f\n", 5 / 2);
    return (0);
}
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144

6 Answers6

7

5 / 2 is integer division which yields an int result. However the "%f" printf flag interprets it as a double value, which will cause undefined behaviour.
In your case, it prints 0.0000, but it could be anything, really.

Use floating point operands in order to get a floating point return value.

printf("%f\n", 5.0 / 2.0);
SirDarius
  • 41,440
  • 8
  • 86
  • 100
2

You are printing a integer with %f specifier. Keep in mind that when you divide int by another int the result is again int. Thus 5/2 is the integer 2, which printed with the %f specifier prints 0.0. To avoid this use 5.0/2 this time dividing a double by an integer and thus getting a double result.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • This seems to be the only answer which actually addresses why `0.0` is printed. I already knew that `5/2` is integer division, but to me, it should print `2` and not `0`. This answer clears that up. The others don't seem to address that issue. – nhgrif Nov 04 '13 at 15:54
  • 3
    @nhgrif No explanation is necessary beyond “it is undefined behavior”. Explaining undefined behavior means providing reasons that fail to hold across instructions sets, ABIs, optimization levels or phases of the moon. With my compiler, for instance, the program prints the contents of a register `%xmm0` that was never set and does not contain the integer 2. – Pascal Cuoq Nov 04 '13 at 16:19
1

You are most likey getting a parameter mismatch in:

printf("%f\n", 5 / 2);  

This should be written as:

printf("%d\n", 5 / 2);  // use "%d" for integer.  

Although not completely correct, the rest of your assignment statements do not appear to contribute to your print failure.

ryyker
  • 22,849
  • 3
  • 43
  • 87
1

As Elias has pointed out in his comment every number is int if stated otherwise. That is the key to solution.

In C/C++ 5/2 is int number and equals 2 That is why your :

printf("%f\n", 5 / 2);

is the same as:

printf("%f\n", 2);

But why it prints 0.0 you ask - here is why. When printf is given '%f' flag it will treat a parameter as float.

Now your code disassembly looks / may look like that:

printf("%f\n", 5 / 2);
push        2    
call        dword ptr [__imp__printf (0B3740Ch)]

push 2 moves (usually 32bit 0x2) on the stack

And now the most important how 32 bit value=2 looks (in binary):

(MSB) 00000000 00000000 00000000 00000010 (LSB)

and what this pattern means if treated as 32 bit floating point (according to this):

sign(bit #31) = 0

exponent(next 8 bits ie #30..#23) all = 0 too

fraction(the remaining bits) contains 2(dec)

enter image description here

Now the value should in theory be calculated as:

(-1)^0 * (1+1*2^-22)^(0-127) = 1 * (1+2^-22)^-127

BUT exponent=0x0 which is special case (read link) and is treated as 0.0 - period.

Community
  • 1
  • 1
Artur
  • 7,038
  • 2
  • 25
  • 39
0

No idea what compiler you are using or what options, but try this:

printf("%f\n",(double)(5 / 2));

or this:

printf("%f\n",5.0 / 2.0));
Hogan
  • 69,564
  • 10
  • 76
  • 117
0

The result of the expression you pass to printf is an integer 0 ('5 / 2' is an integer division). To correct it:

printf("%f\n", (float) 5 / 2);

or

printf("%f\n", 5.0 / 2.0);
piokuc
  • 25,594
  • 11
  • 72
  • 102