4
int foo(int c){
    return c;
}

int main(void){
    int a=5,c;
    c = foo(--a) + a; 
}

Will it invoke undefined behavior in C/C++? I think no it won't.

After reading all the answers I can't figure out whether it is undefined behavior or unspecified behavior.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jaya
  • 345
  • 4
  • 6
  • 4
    No, it won't because it won't compile. (Where's the semicolon!) – flight Mar 22 '11 at 09:55
  • 4
    http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points has a lot of information about this. – Steve Jessop Mar 22 '11 at 10:02
  • 3
    Unless you implement a compiler, or a compiler test suite, or your company employs a bunch of language lawyers, I'd question your need to even know that. Yes, there's a few people here on SO who can answer that question (and you'll find more on `comp.lang.c++.moderated` and `comp.std.c++` on Usenet), but there aren't many and even they sometimes disagree on subtle corner cases. Even more importantly, (some) compilers might disagree with them, too. If the code isn't clear-cut enough that, by one look at it, a reasonably experience programmer can say "Yep!" or "Nope!", simplify it and be done. – sbi Mar 22 '11 at 10:04
  • I answered wrong to prove a point :) – Matt Joiner Mar 22 '11 at 10:04
  • @Matt: your answer did surprise me! Had me jump in my copy of the FCD to check if I had not skipped over something! @Erik is covering us though :) – Matthieu M. Mar 22 '11 at 10:08

5 Answers5

16

Yes it's undefined behavior - a and foo(--a) can be evaluated in any order.

For further reference, see e.g. Sequence Point. There's a sequence point after the complete expression, and after evaluation of the argument to foo - but the order of evaluation of subexpressions is unspecified, per 5/4:

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

EDIT: As Prasoon points out, the behavior is unspecified due to the order of evaluation ... is unspecified., and becomes undefined due to the prior value shall be accessed only to determine the value to be stored

Erik
  • 88,732
  • 13
  • 198
  • 189
  • From what I can understand, it's even worse. Nothing would prevent `a` from being evaluated after `--a` but before `foo(...)`, which could matter if `foo` further alters the value of `a`. – Matthieu M. Mar 22 '11 at 10:06
  • 1
    @Eric : This is not the only reason. For example `int c = foo(k--) + foo(--j)` doesn't invoke undefiend behaviour even though the order of evaluation of `+` is unspecified. – Prasoon Saurav Mar 22 '11 at 10:13
  • @Eric : The behaviour is not only unspecified but it is undefined also. Read my answer. :) – Prasoon Saurav Mar 22 '11 at 10:17
5

You should read this, it will tell you that your code is undefined because + is not a sequence point and as such it is undefined whether f(--a) or a is evaluated first.

filmor
  • 30,840
  • 6
  • 50
  • 48
  • 3
    Or read Prasoon's excellent ___[FAQ entry](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points)___ on the matter. – sbi Mar 22 '11 at 10:06
3

Even though the operands of + operator can be evaluated in either order the behaviour is undefined because it violates the 2nd rule

1) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.

2) Furthermore, the prior value shall be accessed only to determine the value to be stored.

The following is well defined

c = foo(a-1) + a ;

Read this FAQ entry for a better understanding of undefined behaviour and sequence points.

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
2

According to Wikipedia + is not a sequence point, so the order of evaluation is not fixed, hence you have undefined behavior.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
-1

You will get warning for return type in main function else it is ok and c = 8 at the end of main().

Hiren
  • 341
  • 1
  • 4
  • 17