printf

can anyone explain how printf works. is it's output implementation dependent? and also kindly explain the output for the following code.
# include <stdio.h>
void main()
{
      int i=10;
      printf ("%d %d %d",i,++i,++i);
}
i got
12 12 11
i don't understand how it works. kindly help.
thanx.
LVL 1
darpangoelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
I'm not sure about your question about "output implementation dependant". However, the output is readily explainable.

In C, arguments are pushed onto the stack right to left. That is the rightmost argument goes onto the stack first, then the one next to it, etc. and the first argument is pushed last. This means that the called function finds the first argument at the "stack frame" (the spot the stack will be popped to when it needs to return).

So, since i is 10, the first value pushed onto the stack will be 11 (10, preincremented), the second will be 12 (preincremented again), then 12 again, then the format specifier. And so it will print exactly what you saw: 12 12 11.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DexstarCommented:
darpangoel:

> can anyone explain how printf works. is it's output implementation dependent?

The page explains it best:
http://computer.howstuffworks.com/c2.htm

It has a section that covers printf exclusively...
 
> and also kindly explain the output for the following code.

imladris already covered it, so I'm not going to repeat it...  :)


Hope That Helps,
Dex*
0
darpangoelAuthor Commented:
but supposedly if you run the same code in vc .net the output is 12 12 12. how is it so?
(i haven't tried it on vc .net but thats what i was told.)
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference ithrough the Colors of STEM program.

Gratch06Commented:
I'm not particularly certain why that program outputs 12, 12, 11, as I am a learning C coder myself, but I can explain a bit on printf.

The basic syntax of printf is:    printf("<formatted output>", var1, var2, var3, var4);
Some of the switches I use routinely for printf are %d (integer), %e (scientific notation), %f (double, floating point), %c (character), and %s (string of characters).

The C compiler will basically go from left to right matching matching up fields with the variable arguments you give.

For instance, printf("%d<>%e<>%c", 3, 345.6, 'c'); would ouput :  3<>3.456000e+002<> c

Hope that helps a little with the primary question.  I'm lost on finding an answer to the second one.
0
Gratch06Commented:
>>So, since i is 10, the first value pushed onto the stack will be 11 (10, preincremented), the second will be 12 (preincremented again), then 12 again, then the format specifier. And so it will print exactly what you saw: 12 12 11.

That makes perfect sense.  Thanks for the clear explanation imladris.
0
DexstarCommented:
On VS.NET, you will get 12 12 12!

It has to do with how the compiler puts the variables onto the stack, and the order in which they are evaluated, so yes, it is compiler specific.

Dex*
0
mtmikeCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/expre_11.asp

"Function-call operator. All arguments to a function are evaluated and all side effects complete before entry to the function. No order of evaluation among the arguments is specified."

So, the order of evaluation of i, ++i and ++i is unspecified. The possible outcomes should be:

10, 11, 12 (1,2,3)
10, 12, 11 (1,3,2)
11, 11, 12 (2,1,3)
12, 11, 12 (2,3,1)
11, 12, 11 (3,1,2)
12, 12, 11 (3,2,1)

But not 12, 12, 12.

Imladris explained why 12, 12, 11 is the most likely order.
0
DexstarCommented:
mtmike,

I compiled it on VS.NET 2002 and it gives you 12, 12, 12!  Try it yourself.

Dex*
0
grg99Commented:
If it prints out 12,12,12 then it looks like it is doing all the pre-increments first, then pushing the values.  
Doesnt sound right.  Weird.
0
imladrisCommented:
I would assume that the VC.NET compiler is in fact a C++ compiler. It may be that there are different rules for C++.

Also, this kind of thing (referring to i multiple times, some of which are pre (or post) incremented and some not) is often compiler dependant (i.e. left to the compiler implementors for decisions). So it is unwise to ever code such things in real life.
0
DexstarCommented:
I looked it up, and according to the C standard, the compiler's behaviour is undefined...  So don't write code that depends on those parameters being evaluated in a certain order.

Dex*
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.