# Printf Doubt



## mail4kaja (Feb 27, 2008)

Hello,

Recently, I came across a C Program. Its confusing to me. Please clarify how this program gives the output.


```
#include <stdio.h>
int main()
{
 int i = 2;
 float a = 4;
 printf("%f %d\n",i/a,i/a);
 printf("%d %f\n",i/a,i/a);
 return 0;
}
```

i/a = 2/4 = 0.5
SO.. I expect the following as output

```
0.5 0
0 0.5
```

But, I got 
	
	



```
0.5 0
0 0.0000000
```

WHY is this???

Please clarify my doubt..

Thanks in advance,


----------



## Sykora (Feb 27, 2008)

I think that if the numerator is integer, it will use integer division. Not sure though, and it would be kind of stupid.


----------



## Pathik (Feb 27, 2008)

Gives me 
0.500000 0
0 0.000000

In Dev C++
Which I suppose is correct and expected.
Cos the default float precision(that's wat it is called, rite??) is 6.


----------



## QwertyManiac (Feb 27, 2008)

@Sykora - Nope, don't think thats the case.

Int / Int = Int
Int / Float = Float
Float / Int = Float
Float / Float = Float, just as usual! (Its shown by his first line of o/p)

@Pathik - He wasn't talking about precision actually, he is asking why at one o/p the %f is shown as the right 0.50000 while the other shows 0.00000 for the SAME calculation.

I have no answers, either.


----------



## nvidia (Feb 27, 2008)

In printf instead of using "%f" use "%.2f" or "%2f"
Either of the two must return the answer with 2 digits after the decimal point.


----------



## Sykora (Feb 27, 2008)

@Qwerty : I totally missed that it was the _same_ calculation. What was I thinking?


----------



## nvidia (Feb 27, 2008)

^^Im no pro in C... I know only basics... Untill function calls... So i know the smaller stuff in C


----------



## QwertyManiac (Feb 28, 2008)

C/C++ are weird languages with weirder compilers 

In Python, it shows the right things:

```
>>> a = 2
>>> i= 4.0
>>> print "%d %f" %(i/a,i/a)
2 2.000000
>>> print "%f %d" %(i/a,i/a)
2.000000 2
```


----------



## Pathik (Feb 28, 2008)

Hav ny idea why it happens?? Qwerty??


----------



## FilledVoid (Feb 28, 2008)

I might sound like an idiot here but here goes. 

In both statements only the first calculations is actually performed. The value of the calculation is then substituted in the second calculation. In the first line.

```
printf("%f %d\n",i/a,i/a);
```
The first calculation gives you 0.500000 and 0 . Here the first i/a is calculated and the value is substituted in the second i/a. Since %d is being used the output remains as 0. 

```
printf("%d %f\n",i/a,i/a);
```

Now for the second calculation, the results are  0  and 0.0000000 . Here the answer 0 is obvious. .5 is fitted into an integer which is automatically converted to a 0. This value is passed over to the enxt calculation which is a float and hence the result 0.000000 . You can try the same theory for any printf statement. You can even see that the result changes if you change the statement from printf("%d %f\n",i/a,i/a); to printf("%d %f\n",a,i/a); As long as the calculation is varied the answer is given correctly. 

Im not even sure if my theory is correct. So others might want to verify. Hope this helps.   

PS: This question is very interesting . Where did you get it from . Also I'm not sure if this is caused due to a certain Compiler or is a C feature etc. I used Dev-C for this. 

Cheers.


----------



## khattam_ (Feb 28, 2008)

^^ i cannot agree...


```
int i=2;
printf("%d %d",i,i++);
```
outputs

```
2 3
```

whereas

```
int i=2;
printf("%d %d\n",i++,i);
```
outputs 

```
2 2
```

So the order of calculation you are explaining does not look right to me.



@mail4kaja
weird problem
even when I tried new code:

```
#include <stdio.h>
int main()
{
 int i = 2;
 float a = 4;
 float b=i/a;
 printf("%f %d\n",b,b);
 printf("%d %f\n",b,b);
 getchar();
 return 0;
}
```
I get the same output as you did.

wud have bet with anybody who said this wud be the output. Thanks you saved me a lot of money


----------



## QwertyManiac (Feb 28, 2008)

Yes, its a compiler bug alright. What exx_2000 said is sort of true indeed.

Test this program for instance:

```
#include<stdio.h>

int main(void)
{
    float a = 4.0;
    int i = 2;
    float x = i/a;
    (void) printf("%d %f %f %d",x,x,x,x);
    return 0;
}
```

It'll output a funny:

```
0 0.000000 0.000000 1071644672
```

Using %f after a %d to print out the same value creates a problem here.

Even the C++ compiler has this issue.

Trying another variant:

```
#include<stdio.h>

int main(void)
{
    float a = 4.0;
    int i = 2;
    float x = i/a;
    (void) printf("%f %d %d %f",x,x,x,x);
    return 0;
}
```
It again outputs a wrong:
	
	



```
0.500000 0 1071644672 0.500000
```

This leads us to another problem. Using %d successively causes issues too! I think the bug lies with this format specifier (flag?). But the problem's sort of limited to 2 cases only, cause the below code gives wrong output only at every alternative place:


```
#include<stdio.h>

int main(void)
{
	float a = 4.0;
	int i = 2;
	float x = i/a;
	(void) printf("%d %d %d %d",x,x,x,x);
	return 0;
}
```

Outputs:

```
0 1071644672 0 1071644672
```


----------



## mail4kaja (Feb 28, 2008)

*@ALL*
This question is NOT related to precision, and I'm concerned about the value printed.

*@exx_2000*
Your explanation seems to be partially correct.

*@khattam_*
I have already tried using variables to store the value & print. I have even used four variables & tried all the combinations to get the output. When the second printf has i/a as the first expression to be calculated, the second value (even a variable - not a expression) is printed as 0.00000. Weird!!!

*@QwertyManiac*


> Using %f after a %d to print out the same value creates a problem here.


I feel the same. But, is there any  RIGHT way how to explain WHY this is happening?


----------



## Pathik (Feb 28, 2008)

Great explanation exx.. It seems correct atleast now.. 
@khattam you cant compare this program with urs. In ur case the value of the variable itself is changing..


----------



## blueshift (Feb 28, 2008)

khattam_ said:


> ```
> int i=2;
> printf("%d %d",i,i++);
> ```
> ...



It must give 3 2 as the output coz calaculations are done from right side in printf.
Check again.
I tried in Turbo and GCC.


----------



## Pathik (Feb 28, 2008)

Great explanation exx.. It seems correct atleast now.. 
@khattam you cant compare this program with urs. In ur case the value of the variable itself is changing..


----------



## mail4kaja (Feb 28, 2008)

blueshift said:


> It must give 3 2 as the output coz calaculations are done from right side in printf.
> Check again.
> I tried in Turbo and GCC.



Can you please explain a bit more in detail, how we get 3 2 as output ?


----------



## FilledVoid (Feb 28, 2008)

> @exx_2000
> Your explanation seems to be partially correct.



Show em the partial part which is wrong. 



> ^^ i cannot agree...





> printf("%d %d",i,i++);



Why not? Your calculation changes. Refer to your below expression. Place similar expressions int here and run an implicit conversion using the format specifiers. Remember that although they are just format specifiers the data is implicitly converted.

Will respond to rest when I get back on Dev-C


----------



## mail4kaja (Feb 28, 2008)

> The first calculation gives you 0.500000 and 0 . Here the first i/a is calculated and the value is substituted in the second i/a. Since %d is being used the output remains as 0.
> Code:
> 
> printf("%d %f\n",i/a,i/a);
> ...



O..Oh!.. I didn't say that your answer is partially wrong. I did said that your reply same somewhat nearer to the actual reason behind why we got that output.


----------



## FilledVoid (Feb 28, 2008)

I think this whole thing has something to do with the way Float numbers are stored in the memory. Remeber that float numbers are stored as three parts of information. 

Sign 
Mantissa 
Exponent

Somehow by using the same calculation in the same statements the values being shifted or stored in the Integer variables are some how overflowing/underflowing. Could someone verify on VC or some other software. Given some time  think I can find whats going on.


----------



## blueshift (Feb 28, 2008)

mail4kaja said:


> Can you please explain a bit more in detail, how we get 3 2 as output ?




```
int i=2;
printf("%d %d",i,i++);
```
When we code like this, the evaluation happens from right to left i.e. first _i++_ will be done. So for the 2nd %d in printf(), the value of _i_ is still '2'. Then only _i_ is incremented to '3' and displayed in the 1st %d. All this happens during compiling.
If instead the code is:

```
printf("%d %d", i++, i);
```
the result is: 2 2
If you use another printf() after this, then it will display _i_ as '3'.


----------



## khattam_ (Feb 29, 2008)

blueshift said:


> It must give 3 2 as the output coz calaculations are done from right side in printf.
> Check again.
> I tried in Turbo and GCC.



sorry its 

```
3 2
```

just a typing mistake.. hope you got what I was tryin to explain...
so you see your earlier explanation was wrong...



Anyways, I've got the answer when I asked in another forum. 
*forum.mazzako.com/index.php?topic=13291.0


----------

