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

W

WeiWangJi

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

John Harrison

WeiWangJi said:
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
 
V

Vallabha

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

John Harrison

Vallabha said:
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
 
M

Michael DOUBEZ

John Harrison a écrit :
Vallabha said:
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
 
C

Clark Cox

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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top