0

I have a small code in C

#include<stdio.h>
int main()
{
    int a=10,b;
    b=a++ + ++a;
    printf("%d,%d,%d,%d",b,a++,a,++a);
    return 0;
}

Turbo C gives the following Output (as expected)

22,13,13,13

But GCC (used ubuntu and Code blocks compiler in windows) gives the following

22,13,14,14

I believe that Turbo c output was correct, but how come GCC is returning different output?

Gaurav Sharma
  • 745
  • 7
  • 23

2 Answers2

4

They both are correct! This is undefined behavior, because you arent allowed to change the same value in a single invokation multiple times.

From c99 ISO/IEC 9899:TC3 -> Apenndix J:

J.2 Undefined behavior 1 The behavior is undefined in the following circumstances:

[...]

— Between two sequence points, an object is modified more than once, or is modified and the prior value is read other than to determine the value to be stored (6.5).

EDIT:

In reference to icepacks comment who told turbo c is predating c99, I added also the quote from the

C89 standard Programming Language C, X3.???-1988:

A.6.2 Undefined behavior The behavior in the following circumstances is undefined:

[..]

  • An object is modified more than once, or is modified and accessed other than to determine the new value, between two sequence points (3.3).
dhein
  • 6,431
  • 4
  • 42
  • 74
2

This is an undefined behaviour. As in C there is no specification of function argument evaluation, so the compiler is free to do it in any way. It is undefined and arbitrary.

C99 standard 6.5.:-

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • -1 as you cant argument with C++ quotes to c standard question. C++ and C are diferent languages!!! – dhein Sep 14 '13 at 14:40
  • If it is asked in an examination, then I think abrupt nature of c compiler won't help in any way. – Gaurav Sharma Sep 14 '13 at 14:40
  • @GauravSharma:- That would definitely help. You can explain the reason too. Thats an undefined bahavior and its better to get that right to the examiner as well!!! :) – Rahul Tripathi Sep 14 '13 at 14:42
  • @Zaibis:- Thanks for that. Removed the C++ content!! – Rahul Tripathi Sep 14 '13 at 14:42
  • could you tell us please where the quote is from? just itnerested – dhein Sep 14 '13 at 14:45
  • @Zaibis:- Updated that in answer!! :) – Rahul Tripathi Sep 14 '13 at 14:46
  • @RahulTripathi can you explain how the output came `22,13,14,14` in GCC? how the increment of `a` took place and was printed? – Gaurav Sharma Sep 14 '13 at 15:02
  • @GauravSharma:- That is undefined and your compiler takes that in its way. You cannot predict that!!! That is why it is said as **UNDEFINED BEHAVIOR** Hope that makes sense!! – Rahul Tripathi Sep 14 '13 at 15:04
  • The section you quoted isn't what makes the behavior undefined. The relevant citation is section 6.5: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored." (That's C99; C11 uses different wording.) Not that it matters, since the question is a duplicate many times over. – Keith Thompson Sep 14 '13 at 19:34
  • @KeithThompson:- Thanks a lot Sir. Updated the same in my answer!!! – Rahul Tripathi Sep 14 '13 at 19:35