Is the behavior unspecified or can it the expression be evaluated?

C

coolguyaroundyou

Here, analyse this snippet :

{
int a=1,b=0,c=0;


b=a+ ++a;...........Statement 1

a=1;

c=a+ a++;...........Statement 2

}


Both the statements,1 and 2, have side-effects. The standard says that
the order of evaluation of side-effects is unspecified. So,will the
values stored in b and c be implementation-defined?

I tried this in 2 compilers and the result was same, but the result of
the following in the same 2 compilers was different:

{
int a=1;


std::cout<<a<<++a;...........Statement 1

a=1;

std::cout<<a<<a++;...........Statement 2

}

Any reasons for such behavior in this case and also for the behavior
in the former code??
 
J

James Kanze

[...]
Here the behaviour is unspecified because it depends on the
order of evaluation of the function arguments, which itself is
unspecified. It is not undefined, though.

Yes it is. There's still no sequence point between the read of
a in the first sub-expression, and the modification in the
second. A function call introduces a sequence point, but
there's nothing here that would require the operator<< function
to be called between those sub-expressions.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top