question about printf

I

Ips

hello!

here's the code
int main()
{
int i=0;
printf("%d %d %d\n",i++,i++,i++);

return 0;
}

can anybody explain how tthe arguments are passed to this function? Why does
the output is : 3 2 1 and not 1 2 3 ?
Is this ANSI compliant or only gcc compiles it like this?

thanks
regaed,
Ips
 
K

Karl Heinz Buchegger

Ips said:
hello!

here's the code
int main()
{
int i=0;
printf("%d %d %d\n",i++,i++,i++);

return 0;
}

can anybody explain how tthe arguments are passed to this function? Why does
the output is : 3 2 1 and not 1 2 3 ?
Is this ANSI compliant or only gcc compiles it like this?

In short:
It is undefined which output is right.

In long:
You modify a value more then once between sequence points. The result
of doing that is undefined.
This question (or variations of that) come up very often. Please use
google to search the newsgroup archive for a more indepth discussion.
 
R

Rob Williscroft

madhur wrote in in comp.lang.c++:
Hello
The arguments are evaluated from right to left. This is why the output
is 3 2 1. Read about calling conventions.

A common mistake, but nevertheless incorrect. Read about sequence Points.

To the OP, as Karl has already said the output is undefined. In
fact the whole programme exhibts Undefined Behaviour, the standard
nolonger specifies what, if anything, your programme does.

Rob.
 
S

Sumit Rajan

O

Old Wolf

Karl Heinz Buchegger said:
In short:
It is undefined which output is right.

Both outputs are right. The program's output (and any other behavioural
aspects of it) is undefined. You might be thinking of
"implementation-defined" (in which case there would only be one correct
output).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top