?
Solved

printf

Posted on 2003-11-14
11
Medium Priority
?
1,400 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 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

807 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