f



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

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?

0
WeiWangJi (1)
3/9/2007 6:20:37 AM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

6 Replies
993 Views

Similar Articles

[PageSpeed] 25

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
0
3/9/2007 6:29:47 AM
On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.com> 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?

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/

0
3/9/2007 7:02:19 AM
Vallabha wrote:
> On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.com> 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?
> 
> 
> 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
0
3/9/2007 7:12:15 AM
John Harrison a �crit :
> Vallabha wrote:
>> On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.com> 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?
>>
>>
>> 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
0
3/9/2007 8:45:57 AM
On Mar 9, 3:20 pm, "WeiWangJi" <WeiWan...@gmail.com> 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?

This would help you :)
http://www.google.com/search?hl=en&rls=com.microsoft%3Aen-us%3AIE-SearchBox&q=C+puzzles

0
CSarath (129)
3/9/2007 10:49:53 AM
On 2007-03-08 23:02:19 -0800, "Vallabha" <vsnadagouda@gmail.com> said:

> On Mar 9, 11:20 am, "WeiWangJi" <WeiWan...@gmail.com> 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?
> 
> 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 
clarkcox3@gmail.com

0
clarkcox3 (505)
3/9/2007 5:04:10 PM
Reply: