1

I have tried some code in C language but I have encountered this problem.

int i=0;
i=i+++ ++i;   //works fine
//i=i++ +++i;   gives error

My confusion is that how i+++ is running? but +++i return error.

Devavrata
  • 1,785
  • 17
  • 30
  • 1
    Even if the behavior were not undefined, whatever `i=i+++ ++i;` is *supposed* to mean, there's certainly a clearer way to express it, perhaps `int i = 3;` if that happens to be what you meant. – Keith Thompson Jul 23 '14 at 18:47
  • 1
    While it's good to understand why the second line produces an error, this is terrible coding style. Please avoid such coding style. – R Sahu Jul 23 '14 at 18:47
  • @RSahu I am not using this style but just trying to know some facts. – Devavrata Jul 23 '14 at 18:58

2 Answers2

5

C operators are parsed according to the “longest match” rule. Your first example is parsed as:

i = i ++ + ++ i ;

i = (i++) + (++i);

Whereas your second example is parsed as:

i = i ++ ++ + i ;

i = ((i++)++) + i;

The result of the post-increment operator is an rvalue, a copy of the previous value of the variable that was incremented. Applying another post-increment operator to an rvalue is an error because that operator requires an lvalue, intuitively, an expression such as i or *p that can be assigned to.

Also, this code contains undefined behaviour. You are reading i and modifying it without an intervening sequence point;, &&, ||, ,, or ?:—which means that the program behaviour is unpredictable and will vary across compilers.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
  • 2
    Also known as the "maximal munch" rule. – Keith Thompson Jul 23 '14 at 18:46
  • It's not just the result of the expression that's unpredictable; the behavior of the entire program is undefined. – Keith Thompson Jul 23 '14 at 18:49
  • @KeithThompson: That’s true. I meant “the result of the expression” as in “what will happen”, not “the value that will be produced”. Not sure of a better way to phrase that. – Jon Purdy Jul 23 '14 at 18:51
  • You could replace "the result of the expression" by "the behavior of the program". – Keith Thompson Jul 23 '14 at 18:51
  • @JonPurdy how behaviour is undefined ,I always getting 3 as answer. – Devavrata Jul 23 '14 at 19:03
  • @Devavrata: You may observe different results with different optimisation settings, or with different compilers. In `i = i++ + ++i`, either `i++` or `++i` may be evaluated first, and the assignment to `i` may happen before, after, or even between the increments. The result may be any value, or a crash, or whatever the compiler implementor wants to do—compilers are allowed to assume your program contains only defined behaviours, and they may transform the program accordingly. – Jon Purdy Jul 24 '14 at 00:02
1

Both of those statements yield undefined behavior. However, the reason the first works fine is because your compiler interprets it as (i++)+ ++i;, whereas the second line is i++ ++(+i), which makes no sense.

wolfPack88
  • 4,163
  • 4
  • 32
  • 47