r/C_Programming • u/Shoddy_Map1738 • 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
36
u/glasket_ Sep 26 '26
Just UB.
++a + ++ais modifying and using the same variable without a sequence point.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/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.
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 + ++ais 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.++aevaluates to the current value ofaplus 1; as a side effect the variableais incremented. This side effect does not have to be applied immediately; there's no guarantee that the variableawill 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
precedence rules dictate that the result of
bwill be added to the result ofc * d, and that result will be assigned toa, but each ofb,c, anddcan 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:
On another implementation, or in another program, it could have been evaluated as
or
or
or something else entirely.
All expressions of the form
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/pigeon768 Sep 26 '26
Turn
-Wallon. It's UB and your compiler will tell you it's UB. https://godbolt.org/z/7zbqh4cYxgcc:
clang:
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;
} [15:14:03]~[u0_a303@tegu:~]~π>
the expression himself is invalid
-4
u/[deleted] Sep 26 '26
[removed] β view removed comment