By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

the question about: printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);

P: n/a
int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?

Mar 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
WeiWangJi wrote:
int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?
There is no explaination.

Because you modify the same varaible more than once in the same function
call your code is illegal, and it's behaviour is undefined.

If you tried the same code on a differnt compiler you would probably get
a different result.

You could even see you program crash, because your code is illegal.

john
Mar 9 '07 #2

P: n/a
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.comwrote:
int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?
I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.

cut down the sample to analyze the things:

#include <stdio.h>

int main()
{
int i =8;
printf("%d\t %d\t %d\n", i,--i, i--);
printf("%d \n", i);
return 0;
}

Output is:
% ./a.out
7 7 8
6

printf calculates the arguments from right to left and prints them
from left to right.. so i-- is computed first, then --i and then i.

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/

Mar 9 '07 #3

P: n/a
Vallabha wrote:
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.comwrote:
>>int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?


I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.

cut down the sample to analyze the things:

#include <stdio.h>

int main()
{
int i =8;
printf("%d\t %d\t %d\n", i,--i, i--);
printf("%d \n", i);
return 0;
}

Output is:
% ./a.out
7 7 8
6

printf calculates the arguments from right to left and prints them
from left to right.. so i-- is computed first, then --i and then i.

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/
printf might be calculating things left to right on your compiler, but
there is no requirement for it to do so

I ran the OP code on my compiler and got this

7 7 7 8 7 -8
7

I ran it with optmisation turned on and got this

8 8 8 8 8 -8
7

I tried your code on my compiler and got this

6 6 8
6

but with optmisation turned on this

7 7 8
6

All this results are correct, none of the compilers have bugs, because
quite simply the behaviour of the OP's program, and your program, is
undefined by the C++ standard. One can of course provide an explaination
why one particular compiler produces one particular output, but I don't
think it's particularly useful to do so.

john
Mar 9 '07 #4

P: n/a
John Harrison a écrit :
Vallabha wrote:
>On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.comwrote:
>>int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?


I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.

cut down the sample to analyze the things:
[snip]

printf calculates the arguments from right to left and prints them
from left to right.. so i-- is computed first, then --i and then i.
>>

printf might be calculating things left to right on your compiler, but
there is no requirement for it to do so
[snip]

All this results are correct, none of the compilers have bugs, because
quite simply the behaviour of the OP's program, and your program, is
undefined by the C++ standard. One can of course provide an explaination
why one particular compiler produces one particular output, but I don't
think it's particularly useful to do so.
No since it is certainly performance related and dependant on strategies
but also compiler flags.

OP: Google for "sequence point c++"
It will explain what is a sequence point and may tell why modifying a
value multiple time in a sequence point is UB.

Michael
Mar 9 '07 #5

P: n/a
On Mar 9, 3:20 pm, "WeiWangJi" <WeiWan...@gmail.comwrote:
int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?
This would help you :)
http://www.google.com/search?hl=en&r...ox&q=C+puzzles

Mar 9 '07 #6

P: n/a
On 2007-03-08 23:02:19 -0800, "Vallabha" <vs*********@gmail.comsaid:
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.comwrote:
>int i=8;
printf("%d\t%d\t%d\t%d\t%d\t%d\n",i,++i,--i,i--,i++,-i--);
printf("%d\n", i);

why the results is:

8 8 7 8 8 -8
7

How to explain?

I guess you want to someone to explain the reason behind this output..
if so, here is some help for you.
You aren't helping; you're confusing the issue. The result of that code
is completely undefined.
cut down the sample to analyze the things:

#include <stdio.h>

int main()
{
int i =8;
printf("%d\t %d\t %d\n", i,--i, i--);
printf("%d \n", i);
return 0;
}

Output is:
% ./a.out
7 7 8
6
.... or anything else.
printf calculates the arguments from right to left and prints them
This is simply not true; there is no defined order in which arguments
to a function are evaluated.
from left to right.. so i-- is computed first, then --i and then i.

--
Clark S. Cox III
cl*******@gmail.com

Mar 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.