1

In C, is a+++b equal to a+b++?

GG on web
  • 21
  • 1
  • 4
  • @StoryTeller It is just one increase operation and the varible is not used twice, I would figure it is not UB. – Kami Kaze Apr 04 '17 at 06:31
  • The 'maximal munch' rule covers this; one place it is described is [Why doesn't `a+++++b` work in C?](http://stackoverflow.com/questions/5341202/why-doesnt-ab-work-in-c). – Jonathan Leffler Apr 04 '17 at 06:37
  • None of the code in the question as currently shown has undefined behaviour. The requirements of the standard are clear. The assignments could/should be written `c = a++ + b;` and `c = a + b++;`. That is how the compiler will interpret them, thanks to the maximal munch rule. – Jonathan Leffler Apr 04 '17 at 06:39
  • 1
    Now the real question is, what does `++a+-+-+-+-+b++` mean? Lets ponder that for hours. – Lundin Apr 04 '17 at 07:00
  • To answer the actual question, `a++ + b` and `a + b++` are indeed equal, if both `a` and `b` start with the same value; it's just that for the second line, the previous `a++` has incremented the starting value of `a`. – Ken Y-N Apr 04 '17 at 08:06

4 Answers4

5

They are and will be equal if you supply the same initial values of the operands.

In your case, the side effect of the first statement (post increment on a) is affecting the second one. Due to the presence of the post-increment in the first expression, a is incremented to 3 before the next statement is executed.

Re-initialize the variables with the same genesis value before calculating the second one.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

You need to check the C operator precedence to understand it.

The confusing thing here is that a+++b may be read as either a + (++b) or as (a++) + b. According to the C operator precedence, it is actually looks like:

int a=2, b=3, c;

c = (a++) + b; // 2+3=5 and 'a' will be 3 after that line
printf("%d\n",c); // c = 5

c = a + (b++); // 3+3=6 and 'b' will be 4 after that line
printf("%d\n",c); // c= 6

From the link above:

++ as sufix has highest priority.

++ as prefix has lower priority.

+ has even lower priority.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • Nitpick: What operator precedence has to do with this? – Sourav Ghosh Apr 04 '17 at 06:02
  • @SouravGhosh - It matters when interpreting `+++`. How would you determine it's a post-increment on a and not a pre-increment on b? – StoryTeller - Unslander Monica Apr 04 '17 at 06:02
  • @SouravGhosh `a+++b` could be read as `a+(++b)` or `(a++)+b`, each of it will give a different result. – Alex Lop. Apr 04 '17 at 06:03
  • @StoryTeller hmmm....interesting, you mind elaborating a little more? I'm sure i'm missing something there. – Sourav Ghosh Apr 04 '17 at 06:03
  • @SouravGhosh - Well. The post-increment has a higher precedence than pre-increment and it associates with the expression on its left, so an expression like `+++` could have a clear meaning to the compiler, even without any spaces. For unary operators, the precedence is usually used to disambiguate such convoluted expressions. – StoryTeller - Unslander Monica Apr 04 '17 at 06:07
  • @AlexLop. can you give me an example where `a+++b` is actually read as `a+(++b)`,? thanks. – Sourav Ghosh Apr 04 '17 at 06:09
  • 4
    @AlexLop. `a + ++b` will be parsed as `a + (++b)` and `a+++b` will be always parsed as `(a++)+b`. It does not depend on operator precedences, the distinction is due to the lexical analysis phase. – Marian Apr 04 '17 at 06:17
  • @SouravGhosh Please note Marian's comment. – Alex Lop. Apr 04 '17 at 06:19
  • @SouravGhosh According to C operator precedence, never. But this is well known to the compiler but only to few programmers. So unintentionally, a programmer may write a+++b meaning a + (++b) but the actual result will be (a++) + b. – Alex Lop. Apr 04 '17 at 06:21
  • @AlexLop. :) I know. The point I'm still unable to get is how that has _anything_ to do with the operator precedence. – Sourav Ghosh Apr 04 '17 at 06:25
  • @SouravGhosh OK, so maybe I misunderstood you. Did you mean why `(a++) + b` is not equal to `a + (b++)`? That is your question? – Alex Lop. Apr 04 '17 at 06:51
  • Expression parsing has nothing to do with operator precedence, which is a higher level concept. C11 6.4/4 determines how this expression is parsed. Therefore this answer is incorrect. – Lundin Apr 04 '17 at 06:52
1
 int a=2, b=3, c;

 c = (a++) + b; // The value for a will be 3 after that line
 printf("%d\n",c); // c = 5

 c = a + (b++); // So here a value is 3 (3+3) =6 after executing this line b value will be 4
  printf("%d\n",c); // c= 6

To avoid this you need to reinitialize the variables

Abi
  • 724
  • 6
  • 22
0
c = a+++b; 

is equivalent

c = a++ + b;

a++ means post-increment, means expression takes the value of a and then increment.

c = a+b++;

is equivalent

 c = a + b++;

b++ means post-increment, means expression takes the value of b and then increment.

If you provide same value in both cases, then both express variable c same.

msc
  • 33,420
  • 29
  • 119
  • 214