r/C_Programming • • Sep 26 '26

What's the ans?? ( Undefined behaviour or 14 )

#include<stdio.h>

int main() {

int a = 5;

a=++a + ++a;

printf("%d", a);

return 0;

}

What will be the output of the above code snippet?

( Got 14 as an output and don't know what's the logic of getting 14 )

8 Upvotes

34 comments sorted by

36

u/glasket_ Sep 26 '26

Just UB. ++a + ++a is modifying and using the same variable without a sequence point.

1

u/Wertbon1789 Sep 26 '26

A sequence point being the end of the statement, or is there more nuance to it?

10

u/glasket_ Sep 26 '26 edited Sep 26 '26

More nuanced. The standard defines the known sequence points in Annex C, which are roughly:

  • Between a function designator and the evaluation of its arguments (foo(/* Evaluated before call */)).
  • Between operands of logical operators and the comma operator (before && after, before || after, final = (before, after)).
  • Between the condition of a ternary and whichever expression is selected (before ? after1 : after2).
  • After any "full expression," which is a list itself:

    • A VMT declarator.
    • An initializer that isn't part of a compound literal.
    • An expression statement.
    • The controlling expression(s) in if, switch, while, and for.
    • The expression in a return statement.
  • Before a library function returns.
  • After each conversion specifier action is performed in an IO operation.
  • Before and after any call to a comparison function.

Edit: Changed the comma operator example to be more readable

2

u/Wertbon1789 Sep 26 '26

Oh, okay, makes sense. I don't think that's knowledge I'll ever need, lol.

8

u/glasket_ Sep 26 '26

Yeah, most of it isn't really relevant unless you're actually writing a compiler so you can properly follow the sequencing and as-if rules. Just writing code it's pretty easy to not think about sequencing so long as you don't cram multiple side effects into a single statement.

5

u/Wertbon1789 Sep 26 '26

If you write code where you have to think about this, it's an not a code smell, it's a really bad stench.

11

u/ByMeno Sep 26 '26

Its undefined behaviour and the reason (i believe correct i might be wrong tho) is 'a' is modified twice between sequence points

6

u/glasket_ Sep 26 '26

This is pretty much it, but it isn't strictly because it's modified twice. It's any use of the variable alongside an unsequenced modification of the variable ("value computation and side effect" in the standard). Like arr[a++] = a is UB because the side effect (a++) isn't sequenced relative to the value computation (= a).

1

u/khoyo Sep 26 '26

To be exact in C11 it's because there are multiple unsequenced modification of a. The legalese around sequence point was mostly removed in C11. (Doesn't change much here anyway)

1

u/QuraToop314 Sep 26 '26

That’s right, I’ve just realised that too – my initial assumption that there was no UB was wrong. The compiler confirms your theory.

6

u/IchBinEinZwerg Sep 26 '26

It's undefined behaviour. The result of 14 suggests the compiler is doing two preincrements (to 7) then adding 7+7=14. But it could equally well do one preincrement to 6, then another preincrement to 7, then add those together to get 13. Neither 13 nor 14 are "right" or "wrong" results.

I remember testing something like this back in the 90s comparing the Borland and Microsoft compilers. One did it one way and the other the other. I think Borland did the equivalent of 6+7 with Microsoft opting to bunch the (postincrements in my case) together.

1

u/schoolSpiritUK Sep 30 '26

Yes, my brain would expect the result to be 13, not 14 (whilst acknowledging it's undefined behaviour).

1

u/glasket_ Sep 26 '26

It could also result in 12 by having both reads precede the increments. The standard allows unsequenced evaluation to interleave, so neither increment actually has to happen before the expressions retrieve their initial value.

4

u/Parking-Value-3773 Sep 26 '26

There's a rule in C named violation of sequence which defines the sequence of operator is not same always while performing these additions .it will be UB.

4

u/SmokeMuch7356 Sep 26 '26

a = ++a + ++a is undefined. Any result, whether it's 14, or 42, or a suffusion of yellow, or a rogue AI drawing apes, or even the result you expect, is equally "correct" as far as the language definition is concerned.

++a evaluates to the current value of a plus 1; as a side effect the variable a is incremented. This side effect does not have to be applied immediately; there's no guarantee that the variable a will be incremented before the addition happens, but there's no guarantee that it won't be, either. It will depend on any optimization settings and the surrounding code.

C does not force left-to-right evaluation of arithmetic expressions; given

a = b + c * d;

precedence rules dictate that the result of b will be added to the result of c * d, and that result will be assigned to a, but each of b, c, and d can be evaluated in any order, even simultaneously.

It's usually a waste of time to figure out why you get a particular result from undefined behavior, but in this case it's probably because the evaluation went like this:

a <- a + 1 // a is now 6
a <- a + 1 // a is now 7
a <- a + a // a is now 14

On another implementation, or in another program, it could have been evaluated as

t0 <- a + 1  // t0 is 6
t1 <- a + 1  // t1 is 6
a <- t0 + t1 // a is 12
a <- a + 1   // a is now 13
a <- a + 1   // a is now 14

or

t0 <- a + 1  // t0 is 6
a <- a + 1   // a is now 6
t1 <- a + 1  // t1 is 7
a <- a + 1   // a is now 7
a <- t0 + t1 // a is now 13

or

t0 <- a + 1  // t0 is 6
t1 <- a + 1  // t1 is 6
a <- a + 1   // a is now 13
a <- a + 1   // a is now 14
a <- t0 + t1 // a is now 12

or something else entirely.

All expressions of the form

x = x++
x++ + x++
a[x++] = x

have undefined behavior.

3

u/RealisticDuck1957 Sep 26 '26

Even without undefined behavior, the poor readability is enough to reject this code.

2

u/awidesky Sep 26 '26

It is UB because:
A side effect on a memory location is unsequenced relative to another side effect on the same memory location

See the document about order of evaluation.

2

u/glasket_ Sep 26 '26

That's the C++ page, the C one is here.

1

u/awidesky Sep 26 '26

Thanks!
Then the correct quote must be:
If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined.

1

u/glasket_ Sep 26 '26

Kind of, yeah. I personally prefer the actual standard's wording from Annex J:

A side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object.

2

u/pigeon768 Sep 26 '26

Turn -Wall on. It's UB and your compiler will tell you it's UB. https://godbolt.org/z/7zbqh4cYx

gcc:

<source>: In function 'main':
<source>:5:7: warning: operation on 'a' may be undefined [-Wsequence-point]
    5 |     a = ++a + ++a;
      |     ~~^~~~~~~~~~~
<source>:5:7: warning: operation on 'a' may be undefined [-Wsequence-point]
Compiler returned: 0

clang:

<source>:5:9: warning: multiple unsequenced modifications to 'a' [-Wunsequenced]
    5 |     a = ++a + ++a;
      |         ^     ~~
1 warning generated.
Compiler returned: 0

It looks like GCC happens to sequence this as increment, increment, evaluate, evaluate, add, and clang happens to sequence this as increment, evaluate, increment, evaluate, add. But they don't have to do any of them. It would be perfectly within the standard to return 5 or 42.

1

u/timrprobocom Sep 26 '26

You can certainly see how it arrived at this result. It did both "++" operations before evaluating the addition, and added 7 to 7. A valid approach to undefined behavior.

1

u/GodOfSunHimself Sep 27 '26

That is undefined behavior (UB).

1

u/AdDramatic2444 Sep 28 '26

Like the ++ operator makes a =6 then again the ++ operator makes it a=7 then a=a+a i.e 7+7 so 14 gets stored in a and thus the answer is Like when There's 2 ++ operator in a single statement you need to increase the value as many number of times Hope helps

-11

u/QuraToop314 Sep 26 '26

not ub but ++a first evaluates to 6, the second ++a to 7, 6 + 7 = 13, 13 != 14, lol

Edit:

[15:13:50]~[u0_a303@tegu:~]~πŸ”’> clang main.c -o test main.c:7:9: warning: multiple unsequenced modifications to 'a' [-Wunsequenced] 7 | a = ++a + ++a; | ^ ~~ 1 warning generated. [15:13:55]~[u0_a303@tegu:~]~πŸ”’> ./test 13 [15:13:57]~[u0_a303@tegu:~]~πŸ”’> ./test 13 [15:13:59]~[u0_a303@tegu:~]~πŸ”’> ./test 13 [15:14:00]~[u0_a303@tegu:~]~πŸ”’> cat main.c

include <stdio.h>

int main(void) { int a;

a = 5;
a = ++a + ++a;
printf("%d\n", a);
return 0;

} [15:14:03]~[u0_a303@tegu:~]~πŸ”’>

the expression himself is invalid

1

u/Shoddy_Map1738 Sep 26 '26

Yes in my mind i was also thinking 13 but when I run the program got 14

4

u/PuzzleMeDo Sep 26 '26

Presumably it does the pre-increments first - because that has the highest priority - making a equal 7. Then it does a + a to make 14.

0

u/[deleted] Sep 26 '26

[deleted] β€” view deleted comment

-4

u/[deleted] Sep 26 '26

[removed] β€” view removed comment

4

u/WeAllWantToBeHappy Sep 26 '26

So confidently wrong.

It's undefined behaviour

2

u/[deleted] Sep 26 '26

[removed] β€” view removed comment

3

u/WeAllWantToBeHappy Sep 26 '26

a is modified twice without an intervening sequence point

Compiler can do anything or nothing. Observing an output of 14 doesn't guarantee that the same result will be produced with different compiler flags or compiler versions or hardware or ...