2

I'm trying to understand why my program won't to compile,

int myfunction(int x)
{
   return x;
} 

int main(){
  int x = 10;
  int result=0;
  result=myfunction(x) * myfunction(++x);
  printf("Result is = %d", result);
}

After execution i get : warnings being treated as errors In function 'int main()': operation on 'x' may be undefined. Someone has an idea ?

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
timmz
  • 2,194
  • 3
  • 23
  • 29
  • 1
    dupe of http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points and followups – Balog Pal Jun 17 '13 at 12:19

4 Answers4

5

myfunction(x) * myfunction(++x) is undefined because the order of evaluation of the two arguments to operator * is unspecified. So either the first or second call can be executed first, meaning that theoretically x or ++x can be evaluated first, which can lead to different results. Theoretically. In practice, the standard just passes responsibility to you not to do this.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @Lucian: is that means I can't use ++Var in function arguments ? – timmz Jun 17 '13 at 12:28
  • 1
    @Timmz you can, as long as you only read from `Var` once between sequence points. Read the suggested duplicate, it explains the reasoning really well. – Luchian Grigore Jun 17 '13 at 12:29
  • @Timmz: you should read the whole c-faq (come from the time Usenet was the only place where gurus on a subject went, and thus it's very complete and refined) : http://c-faq.com/expr/seqpoints.html is the section about sequence points (I do recommend to read the whole faq, though, especially sections about pointers, arrays, and NULL, etc) – Olivier Dulac Jun 17 '13 at 15:26
4

You are using undefined behavior.

There's no guarantee that the expression myfunction(x) * myfunction(++x) is evaluated in any particular order, and since it has side-effects its behavior is undefined.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

Your code used "Undefined behaviour".

The C and C++ standards does not state (or define) in which order x and ++x myfunction(x) and myfunction(++x) should be evaluated. Do you expect the result to be 121 or 110 (or soemthing else entirely) - since both of those values are perfectly valid results, would you be equally happy having both results, or do you think one is "more accurate" than the other?

The compiler is warning you that you can't expect this code to produce whichever you prefer of the possible results (which may include something you think "isn't possible"), and a different compiler (or different settings/version for your current compiler) may result in a different value.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 3
    Can somone please explain why my answer is downvoted - did I mess up with C standard when it's a C++ question (now fixed, since it applies to both), or something else? It REALLY annoys me when someone downvotes with no explanation... – Mats Petersson Jun 17 '13 at 12:18
  • I agree with you, sometimes users press downvote for no particular reasons. – timmz Jun 17 '13 at 12:25
  • your answer confuses undefined and unspecified what makes a big difference in theory and shall be known even if unspecified is also unacceptable for most programming – Balog Pal Jun 17 '13 at 12:30
0

The ++ operator has a side effect that makes the statement undefined.

You can do like this :

    result = myfunction(x) * myfunction(x + 1);

    ++i; /* or i++ in this case, doesnt matter */

    printf("Result is = %d", result)
3wic
  • 500
  • 6
  • 22