Thread: Need Some explanation for this program output

1. Need Some explanation for this program output

Hi,

I am returning to C programs after a long time and i have confusion why 3rd output is 31! Remembering concepts of precedence and association i think i am either overlooking something or it may be undefined behavior. But i am 70% sure that it may not be come under criteria of undefined behavior and this is why posting it here to understand it.

Code:
```If p = 10 then
(++p) + p + p = 33
p + (++p) + p = 33
p + p + (++p) = 31
p + p * (++p) = 132```

2. I'm pretty sure all that nonsense results in undefined behaviour.

3. I'm pretty sure I agree with Salem

Edit: gcc even warns you about it :/

Code:
```#include <stdio.h>

int main(void)
{
int p = 10;
int a, b, c, d;

a = (++p) + p + p;
b = p + (++p) + p;
c = p + p + (++p);
d = p + p * (++p);

return 0;
}```
Code:
```dumb.c: In function main:
dumb.c:8:10: warning: operation on p may be undefined [-Wsequence-point]
8 |     a = (++p) + p + p;
|         ~^~~~
dumb.c:8:10: warning: operation on p may be undefined [-Wsequence-point]
dumb.c:9:14: warning: operation on p may be undefined [-Wsequence-point]
9 |     b = p + (++p) + p;
|             ~^~~~
dumb.c:9:14: warning: operation on p may be undefined [-Wsequence-point]
dumb.c:10:18: warning: operation on p may be undefined [-Wsequence-point]
10 |     c = p + p + (++p);
|                 ~^~~~
dumb.c:11:18: warning: operation on p may be undefined [-Wsequence-point]
11 |     d = p + p * (++p);
|                 ~^~~~
dumb.c:11:18: warning: operation on p may be undefined [-Wsequence-point]```

4. Ok. Understood. Thanks a lot guys for clearing out my confusion.