Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 27-02-2008, 03:37 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Location: Chennai
Posts: 5
Question Printf Doubt


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,
__________________
R Kaja Mohideen
mail4kaja is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 27-02-2008, 06:45 PM   #2 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Printf Doubt

I think that if the numerator is integer, it will use integer division. Not sure though, and it would be kind of stupid.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 27-02-2008, 07:22 PM   #3 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: Printf Doubt

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.
__________________
My new blog: www.pathikshah.com
Pathik is offline  
Old 27-02-2008, 08:45 PM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Printf Doubt

@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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 27-02-2008, 08:48 PM   #5 (permalink)
-----ATi-----
 
nvidia's Avatar
 
Join Date: May 2007
Location: Bangalore
Posts: 2,322
Default Re: Printf Doubt

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.
__________________
http://twitter.com/akshayms
nvidia is offline  
Old 27-02-2008, 08:51 PM   #6 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default Re: Printf Doubt

@Qwerty : I totally missed that it was the _same_ calculation. What was I thinking?
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
Sykora is offline  
Old 27-02-2008, 08:53 PM   #7 (permalink)
-----ATi-----
 
nvidia's Avatar
 
Join Date: May 2007
Location: Bangalore
Posts: 2,322
Default Re: Printf Doubt

^^Im no pro in C... I know only basics... Untill function calls... So i know the smaller stuff in C
__________________
http://twitter.com/akshayms
nvidia is offline  
Old 28-02-2008, 12:15 AM   #8 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Printf Doubt

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
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 28-02-2008, 12:39 AM   #9 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: Printf Doubt

Hav ny idea why it happens?? Qwerty??
__________________
My new blog: www.pathikshah.com
Pathik is offline  
Old 28-02-2008, 02:00 AM   #10 (permalink)
Who stole my Alpaca!
 
FilledVoid's Avatar
 
Join Date: Jan 2005
Location: Kerala
Posts: 2,020
Default Re: Printf Doubt

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.
__________________
The Ultimate Chess Strategy : "Hit Hard, Hit Fast and Hit Often"
FilledVoid is offline  
Old 28-02-2008, 04:27 AM   #11 (permalink)
Fresh Stock Since 2005
 
Join Date: Feb 2005
Posts: 1,015
Default Re: Printf Doubt

^^ 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
__________________
http://www.khattam.info

Last edited by khattam_; 28-02-2008 at 04:33 AM.
khattam_ is offline  
Old 28-02-2008, 05:29 AM   #12 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Printf Doubt

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
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 28-02-2008, 08:24 AM   #13 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Location: Chennai
Posts: 5
Cool Re: Printf Doubt

@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
Quote:
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?
__________________
R Kaja Mohideen
mail4kaja is offline  
Old 28-02-2008, 09:17 AM   #14 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: Printf Doubt

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..
__________________
My new blog: www.pathikshah.com
Pathik is offline  
Old 28-02-2008, 09:40 AM   #15 (permalink)
Wise Old Crow
 
blueshift's Avatar
 
Join Date: Apr 2005
Location: Inside the Pixel
Posts: 1,227
Default Re: Printf Doubt

Quote:
Originally Posted by khattam_ View Post
Code:
int i=2;
printf("%d %d",i,i++);
outputs
Code:
2 3
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.
__________________
http://twitter.com/blueshift155
blueshift is offline  
Old 28-02-2008, 09:48 AM   #16 (permalink)
Google Bot
 
Pathik's Avatar
 
Join Date: Aug 2005
Posts: 9,772
Default Re: Printf Doubt

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..
__________________
My new blog: www.pathikshah.com
Pathik is offline  
Old 28-02-2008, 11:29 AM   #17 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Location: Chennai
Posts: 5
Default Re: Printf Doubt

Quote:
Originally Posted by blueshift View Post
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 ?
__________________
R Kaja Mohideen
mail4kaja is offline  
Old 28-02-2008, 11:32 AM   #18 (permalink)
Who stole my Alpaca!
 
FilledVoid's Avatar
 
Join Date: Jan 2005
Location: Kerala
Posts: 2,020
Default Re: Printf Doubt

Quote:
@exx_2000
Your explanation seems to be partially correct.
Show em the partial part which is wrong.

Quote:
^^ i cannot agree...
Quote:
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
__________________
The Ultimate Chess Strategy : "Hit Hard, Hit Fast and Hit Often"
FilledVoid is offline  
Old 28-02-2008, 11:38 AM   #19 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Location: Chennai
Posts: 5
Default Re: Printf Doubt

Quote:
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.
__________________
R Kaja Mohideen
mail4kaja is offline  
Old 28-02-2008, 12:59 PM   #20 (permalink)
Who stole my Alpaca!
 
FilledVoid's Avatar
 
Join Date: Jan 2005
Location: Kerala
Posts: 2,020
Default Re: Printf Doubt

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.
__________________
The Ultimate Chess Strategy : "Hit Hard, Hit Fast and Hit Often"
FilledVoid is offline  
Old 28-02-2008, 09:42 PM   #21 (permalink)
Wise Old Crow
 
blueshift's Avatar
 
Join Date: Apr 2005
Location: Inside the Pixel
Posts: 1,227
Default Re: Printf Doubt

Quote:
Originally Posted by mail4kaja View Post
Can you please explain a bit more in detail, how we get 3 2 as output ?
Code:
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:
Code:
printf("%d %d", i++, i);
the result is: 2 2
If you use another printf() after this, then it will display i as '3'.
__________________
http://twitter.com/blueshift155
blueshift is offline  
Old 29-02-2008, 02:52 AM   #22 (permalink)
Fresh Stock Since 2005
 
Join Date: Feb 2005
Posts: 1,015
Default Re: Printf Doubt

Quote:
Originally Posted by blueshift View Post
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
Code:
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.
http://forum.mazzako.com/index.php?topic=13291.0
__________________
http://www.khattam.info
khattam_ is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
?using single printf statement to print 2 answers? slugger Programming 8 01-02-2008 03:02 PM
Doubt on Ram aneeshkj Hardware Q&A 5 18-04-2007 08:32 PM
Ram doubt aneeshkj Hardware Q&A 4 14-04-2007 04:56 PM
hdd doubt... simoncherian QnA (read only) 4 12-06-2006 12:17 AM
VB Doubt ? Dipen01 QnA (read only) 12 22-08-2005 12:46 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:52 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2