Printf Doubt

Status
Not open for further replies.

mail4kaja

Right off the assembly line
Hello,

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

Code:
#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
Code:
0.5 0
0 0.5

But, I got
Code:
0.5 0
0 0.0000000

WHY is this???

Please clarify my doubt..

Thanks in advance,
 

Sykora

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

Pathik

Google Bot
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

Commander in Chief
@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

-----ATi-----
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

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

nvidia

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

QwertyManiac

Commander in Chief
C/C++ are weird languages with weirder compilers :))

In Python, it shows the right things:
Code:
>>> 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
 
F

FilledVoid

Guest
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.
Code:
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.
Code:
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.
 
K

khattam_

Guest
^^ i cannot agree...

Code:
int i=2;
printf("%d %d",i,i++);
outputs
Code:
2 3

whereas
Code:
int i=2;
printf("%d %d\n",i++,i);
outputs
Code:
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:
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 :D
 
Last edited by a moderator:

QwertyManiac

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

Test this program for instance:
Code:
#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:
Code:
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:
Code:
#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:
Code:
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:

Code:
#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:
Code:
0 1071644672 0 1071644672
 
OP
M

mail4kaja

Right off the assembly line
@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

Google Bot
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..
 

Pathik

Google Bot
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..
 
F

FilledVoid

Guest
@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
 
OP
M

mail4kaja

Right off the assembly line
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);

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 .

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.
 
F

FilledVoid

Guest
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.
 
Status
Not open for further replies.
Top Bottom