Solved

printf

Posted on 2003-11-14
11
1,391 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

744 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

12 Experts available now in Live!

Get 1:1 Help Now