Solved

printf

Posted on 2003-11-14
11
1,392 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:darpangoel
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 9750163
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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9750206
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
 
LVL 1

Author Comment

by:darpangoel
ID: 9750212
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
 
LVL 1

Expert Comment

by:Gratch06
ID: 9750241
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
 
LVL 1

Expert Comment

by:Gratch06
ID: 9750249
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Expert Comment

by:Dexstar
ID: 9750273
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
 
LVL 5

Expert Comment

by:mtmike
ID: 9750478
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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9750504
mtmike,

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

Dex*
0
 
LVL 22

Expert Comment

by:grg99
ID: 9750914
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
 
LVL 16

Expert Comment

by:imladris
ID: 9751137
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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9751146
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now