25 comments

  • susam18 hours ago
    The code in the post seems very similar to the one in my own post from 2010: <a href="https:&#x2F;&#x2F;susam.net&#x2F;sequence-points.html" rel="nofollow">https:&#x2F;&#x2F;susam.net&#x2F;sequence-points.html</a><p><pre><code> int a = 5; a += a++ + a++; </code></pre> I do remember that this particular code snippet (with a = 5, even) used to be popular as an interview question. I found such questions quite annoying because most interviewers who posed them seemed to believe that whatever output they saw with their compiler version was the correct answer. If you tried explaining that the code has undefined behaviour, the reactions generally ranged from mild disagreement to serious confusion. Most of them neither cared about nor understood &#x27;undefined behaviour&#x27; or &#x27;sequence points&#x27;.<p>I remember one particular interviewer who, after I explained that this was undefined behaviour and why, listened patiently to me and then explained to me that the correct answer was 17, because the two post-increments leave the variable as 6, so adding 6 twice to the original 5 gives 17.<p>I am very glad these types of interview questions have become less prevalent these days. They have, right? Right?
    • kentm1 hour ago
      IMO, The only reasonable answer if asked this in an interview is “I would not write code where I have to know the answer to this question”<p>These sorts of things are neat trivia to learn about things like sequence points but 99.9% of the time if it matters in your codebase you&#x27;re writing something unmaintainable.
      • tzs5 minutes ago
        &gt; IMO, The only reasonable answer if asked this in an interview is “I would not write code where I have to know the answer to this question”<p>That&#x27;s half of a reasonable answer. The other half is &quot;but I do know the answer so if I see it when reviewing or working on someone else&#x27;s code I can flag it or rewrite it, and explain to them why it is bad&quot;.
    • LPisGood1 hour ago
      In some sense, and without the interviewer knowing, that is actually a great scenario for an interview.<p>If you can convince someone in a position of authority that they’re wrong about something technical without upsetting them then you’re probably a good culture fit and someone who can raise the average effectiveness of your team.
      • rcxdude1 hour ago
        Or, also, in the reverse direction, if the interviewer is wrong about it and can&#x27;t be convinced otherwise, it&#x27;s probably not a great place to work.
      • bluGill1 hour ago
        I know I did recommend someone after the interview because I looked it up and they were right. Great person to work with. Though I fully understand why most would hesitate.
      • wat1000041 minutes ago
        The best interview questions spawn discussions. This is a pretty good one for that. We could dive into what makes it UB, why a particular compiler might do it a certain way, what results we&#x27;d likely see from other compilers, and why the standard might say that this sort of thing is UB.<p>&quot;What does this produce?&quot; and expecting an answer of &quot;17&quot; is a bad question even if UB didn&#x27;t mean the expected answer is wrong.
        • LPisGood36 minutes ago
          I don’t work a ton with C, but I wonder how C programmers keep track of what behavior is and is not defined. It seems like there are many possible edge cases.
          • wat1000027 minutes ago
            We get by on a combination of matching patterns (any pointer cast gets a lot of scrutiny, for example), compiler warnings, tools like UBSan, debugging when things go wrong, and sheer dumb luck.<p>Having an understanding of how the code gets transformed into machine code helps. For this case, there&#x27;s the basic idea that `a++` will boil down to three basic conceptual operations: fetch, add, and store, and those can be potentially interleaved with other parts of the statement. In something like `a++ + ++b` the interleaving doesn&#x27;t affect the outcome no matter how it&#x27;s done. In `a++ + ++b` the interleaving can affect the outcome, and that&#x27;s your sign that something might be wrong.<p>Any memory safety issue in C code had to involve UB at some point. And you can see how prevalent those are, and deduce how not-particularly-great we are at keeping track of UB.
    • tete17 minutes ago
      &gt; I found such questions quite annoying because most interviewers who posed them seemed to believe that whatever output they saw with their compiler version was the correct answer.<p>Other than the job for most programmers having nothing to do with whether they know the outcome, because hopefully they&#x27;d never write something like it or clean it up. And IF they found it they&#x27;d hopefully test it - given that it appears to be compiler dependent anyways.
    • mike_hock1 hour ago
      Do you want a job at a place where someone who doesn&#x27;t understand UB makes the hiring decisions?
      • ketzu23 minutes ago
        I think your options are very limited if you look for places that have people that truly understand UB, even less so the hiring people.
      • grahamburger22 minutes ago
        Sometimes, even in tech, you just need a job.
    • p0w3n3d47 minutes ago
      How many tennis balls can fit in a bus?
    • SilasX33 minutes ago
      Heh, one time when I got this style of question[1] (but for JavaScript), I took a glance at it and said &quot;Um ... you really shouldn&#x27;t write code like that.&quot; The interviewer replied, &quot;Oh. Yeah. Fair point.&quot; And then went on to another question.<p>[1] By which I mean predicting the behavior of error-prone code that requires good knowledge of all the quirks of the language to correctly answer.
    • colechristensen2 hours ago
      &gt;I am very glad these types of interview questions have become less prevalent these days. They have, right? Right?<p>I just refuse to do interviews like that any more.
  • Aardwolf2 hours ago
    What&#x27;s the reason that C didn&#x27;t define the order of this?<p>The horrible undefined behavior of signed integer overflow at least can be explained by the fact that multiple CPU architectures handling those differently existed (though the fact that C even &#x27;attracts&#x27; its ill-defined signed integers when you&#x27;re using unsigned ones by returning a signed int when left shifting an uint16_t by an uint16_t for example is not as forgivable imho)<p>But this here is something that could be completely defined at the language level, there&#x27;s nothing CPU dependent here, they could have simply stated in the language specification that e.g. the order of execution of statements is from left to right (and&#x2F;or other rules like post increment happens after the full statement is finished for example, my point is not whether the rule I type here is complete enough or not but that the language designers could have made it completely defined).
    • jcranmer1 hour ago
      The short answer is because C was designed to give leeway to really dumb compilers on really diverse hardware.<p>This isn&#x27;t quite the same case, but it&#x27;s a good illustration of the effect: on gcc, if you have an expression f(a(), b()), the order that a and b get evaluated is [1] dependent on the architecture and calling-convention of f. If the calling convention wants you to push arguments from right to left, then b is evaluated first; otherwise, a is evaluated first. If you evaluate arguments in the right order, then after calling the function, you can immediately push the argument on the stack; in the wrong order, the result is now a live variable that needs to be carried over another function call, which is a couple more instructions. I don&#x27;t have a specific example for increment&#x2F;decrement instructions, but considering extremely register-poor machines and hardware instruction support for increment&#x2F;decrement addressing modes, it&#x27;s not hard to imagine that there are similar cases where forcing the compiler to insert the increment at the &#x27;wrong&#x27; point is similarly expensive.<p>Now, with modern compilers using cross-architecture IRs as their main avenue of optimization, the benefit from this kind of flexibility is very limited, especially since the penalties on modern architectures for the &#x27;wrong&#x27; order of things can be reduced to nothing with a bit more cleverness. But compiler developers tend to be loath to change observable behavior, and the standards committee unwilling to mandate that compiler developers have to modify their code, so the fact that some compilers have chosen to implement it in different manners means it&#x27;s going to remain that way essentially forever. If you were making a new language from scratch, you could easily mandate a particular order of evaluation, and I imagine that every new language in the past several decades has in fact done that.<p>[1] Or at least was 20 years ago, when I was asked to look into this. GCC may have changed since then.
      • wat1000031 minutes ago
        I&#x27;d say it&#x27;s more like C was designed <i>from</i> really dumb compilers on really diverse hardware. The standard, at least the early versions of it, was more to codify what was out there than to declare what was correct. For most things like this in the standard, you can point to two pre-standardization compilers that did it differently.
        • AnimalMuppet15 minutes ago
          Kind of both? There were pre-standard compilers, but when they created the standard, they tried to make it so that one could write really dumb compilers and still fulfill the standard.
    • fweimer1 hour ago
      Sethi-Ullman register allocation reorders subexpression evaluation to achieve efficient register allocation: <a href="https:&#x2F;&#x2F;dl.acm.org&#x2F;doi&#x2F;10.1145&#x2F;321607.321620" rel="nofollow">https:&#x2F;&#x2F;dl.acm.org&#x2F;doi&#x2F;10.1145&#x2F;321607.321620</a><p>With modern register allocators and larger register sets, code generation impact from following source evaluation is of course lower than it used to be. Some CPUs can even involve stack slots in register renaming: <a href="https:&#x2F;&#x2F;www.agner.org&#x2F;forum&#x2F;viewtopic.php?t=41" rel="nofollow">https:&#x2F;&#x2F;www.agner.org&#x2F;forum&#x2F;viewtopic.php?t=41</a><p>On the other hand, even modern Scheme leaves evaluation order undefined. It&#x27;s not just a C issue.
    • marcosdumay2 hours ago
      Applying the increment or decrement operators over the same variable more than once on the same line should be a compile-time error.<p>Anyway, yes, this one example has an obvious order it should be applied. But still, something like it shouldn&#x27;t be allowed.
      • nayuki1 hour ago
        &gt; Applying the increment or decrement operators over the same variable more than once on the same line should be a compile-time error<p>That would be nice, but don&#x27;t forget the more general case of pointers and aliasing:<p><pre><code> int a = 5; int *pa = &amp;a; printf(&quot;%d&quot;, (a++ + ++*pa)); </code></pre> The compiler cannot statically catch every possible instance of a statement where a variable is updated more than once.
        • marcosdumay1 hour ago
          Well, aliased updates are undefined behavior already.
      • saghm2 hours ago
        Honestly, having increment in expressions rather than a statement feels like more of a footgun than a benefit. Expressions shouldn&#x27;t mutate things.
        • cesaref1 hour ago
          I think the history of this is that these operations were common with assembly programmers, so when C came along, these were included in the language to allow these developers to feel they weren&#x27;t leaving lots of performance behind.<p>Look at the addressing modes for the PDP-11 in <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;PDP-11_architecture" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;PDP-11_architecture</a> and you&#x27;ll see you can write (R0)+ to read the contents of the location pointed to by R0, and then increment R0 afterwards (so a post increment).<p>Back in the day, compilers were simple and optimisations weren&#x27;t that common, so folding two statements into one and working out that there were no dependencies would have been tough with single pass compilers.<p>You could argue that without such instructions, C wouldn&#x27;t have been embraced quite so enthusiastically for systems programming, and the world would have looked rather different.
        • zarzavat1 hour ago
          Python recently went the other way and added an assignment expression. I actually wish more languages would go further and add statement expressions instead of having to imitate them with IIFEs.<p>C just wouldn&#x27;t be C without things like a[i++]
        • marcosdumay1 hour ago
          Those things are for pointer golf and writing your entire logic inside the if statement.<p>Both are favorite idioms of C developers. And they are ok if done correctly, clearer than the alternative. They are also unnecessary in modern languages, so those shouldn&#x27;t copy it (yeah, Python specifically).
        • kibwen1 hour ago
          In any language where the practice of iteration isn&#x27;t achieved via C-style for-loops, having an operator devoted to increment just doesn&#x27;t make sense (let alone <i>four</i> operators, for each of pre&#x2F;post-increment&#x2F;decrement). This is one of those backwards things that just needs to be chucked in the bin for any language developed post-2010.
          • fc417fc8021 hour ago
            When used well it makes for compact readable code. I don&#x27;t see what it has to do with for loops or operators specifically. For example you can do the same in scheme while iterating by means of tail recursion.
            • kibwen34 minutes ago
              <i>&gt; I don&#x27;t see what it has to do with for loops or operators specifically.</i><p>The reason that these operators pull their weight in C is because iteration over arrays is achieved by manual incrementation (usually via the leading clauses of the for-loop) followed by direct indexing. Languages with a first-class notion of iteration don&#x27;t directly index in this way, which overwhelmingly eliminates not only the vast majority of array indexing operations in codebases but also the need to manually futz with the inductive loop variable. Case in point, Rust doesn&#x27;t have `++` in any form, and it doesn&#x27;t miss it, because Rust has first-class iteration; on the then relatively rare occasion where you want do want to increment, you can do `+=1`, which doesn&#x27;t have the footguns of `++` due to assignment being a statement rather than expression, while leading to a simpler language due to leveraging the existing `+=` syntax rather than needing a whole new set of operators.
              • fc417fc8020 minutes ago
                For loops is hardly the only usecase and built in iteration constructs frequently fall short. For example any mildly complex loop that involves pointer juggling can benefit.<p>&gt; which doesn&#x27;t have the footguns of `++` due to assignment being a statement rather than expression,<p>So then I implement inc( v ) and ... same issue, right? Plus with rust macros is there any technical reason you can&#x27;t trivially implement ++ for yourself? That&#x27;s the case for most lisps that I touched on earlier.
        • throw_await1 hour ago
          Wenn wouldn&#x27;t have pearls like while (<i>dst++ = </i>src++);
    • AlotOfReading2 hours ago
      It&#x27;s valuable for compilers to be able to choose the instruction scheduling order. Standards authors try not to unnecessarily bind implementors. If post increment happened after the full statement is finished, then the original value has to be maintained until the next sequence point. Maybe the compiler will be smart enough to elide that, maybe not, but it&#x27;s a lot more difficult to fix those kinds of edge cases than to say sequencing is undefined.
      • Aardwolf2 hours ago
        But this is not valuable if doing so results in different numerical results, and I think that will always happen if ++ is executed at different times, there&#x27;s no point in a compiler optimizing pointless code that can silently give different results elsewhere
        • AlotOfReading1 hour ago
          Your compiler does many optimizations that break numerical reproducibility, especially in floats. I reviewed a PR the other day that wrote X=A<i>B+(C</i>D)+E;<p>And when I checked 3 different compilers, each of them chose a different way to use FMAs.<p>Even with integer math, you can get different numerical results via UB (e.g. expressions with signed overflow one way and not another).
      • xigoi1 hour ago
        It would only make a difference in cases that are currently UB, so there is no program valid under current C that would be pessimized by this change.
        • AlotOfReading1 hour ago
          It&#x27;s a language feature that was in K&amp;R, and the rules around sequencing were introduced in C89. There were good reasons to believe it would pessimize code in the following decades. Dennis Richie himself pointed out that Thompson probably added the operators because compilers of the time were able to generate better code that way.
    • tardedmeme1 hour ago
      The C standard doesn&#x27;t define things where two or more historical compilers disagreed and there wasn&#x27;t an obviously correct way. This is defined behavior (left to right, assignment last) in Java, which is a different language.
    • rcxdude1 hour ago
      Probably because when C was standardised there were already multiple implementations, and this was an area where implementations differed but it wasn&#x27;t viewed as important enough to bring them in line with one approach.
    • bluGill1 hour ago
      The only other reasonable option is to make such garbage a compile time error. There is no reasonable definition of what code like that should do and if you write it in the real world you need find better job fit. I&#x27;d normally say McDonald&#x27;s is hiring, but they don&#x27;t want people like that either
    • TacticalCoder27 minutes ago
      &gt; What&#x27;s the reason that C didn&#x27;t define the order of this?<p>I didn&#x27;t open TFA but my first thought was <i>&quot;Is this even defined?&quot;</i>.<p>It kinda make sense that suck fucktardedness could be not defined.
  • Timwi23 minutes ago
    The statement is valid C#, which has left-to-right execution order and no undefined behavior. The answer is 5 + 7 = 12.
    • chasil21 minutes ago
      Awk also says it&#x27;s 12.<p><pre><code> awk &#x27;BEGIN{a=5; a = a++ + ++a; print a}&#x27; 12</code></pre>
  • sangeeth961 hour ago
    I had to fight through school and university in India with my teachers who believed these were legit questions to ask in written exams. Can&#x27;t 100% blame them since almost all standard-issue textbooks had them and claimed they&#x27;d give predictable output. I thought the same until I noticed the weirdness when running them across different compilers and after I read about UB, sequence points and similar quirks in books that are not total garbage.<p>Luckily, I ended up with smug smiles in all those cases after showing them the output from different compilers.
  • Boxxed2 hours ago
    &gt; The interesting thing here is the Undefined Behavior (UB), well... actually two UBs, thanks to which there are three possible correct answers: 11, 12 and 13.<p>No, if you invoke undefined behavior any result at all is possible.
    • gynvael7 minutes ago
      Hey! Author here :)<p>So let me start by saying that that blog post was written was 15 years ago and I don&#x27;t even remember the details of it and what I&#x27;ve written there. But, I have a hot-take on this topic you&#x27;ve touched on!<p>From a programmer perspective, you are absolutely right. The behaviour is undefined, end of discussion. A programmer should never rely on what they observe as the effective behaviour of an UB. A programmer must avoid creating situations in code that could result in the execution flow venturing into the areas of UB. And - per C and C++ standards - results of UB can be anything (insert the old joke about UB formatting one&#x27;s disk being a formally correct behaviour).<p>However, I&#x27;m a security researcher, and from the security point of view - especially on the offensive side - we need to know and understand the effective behaviours of UBs. This is because basically all &quot;low-level&quot; vulnerabilities in C&#x2F;C++ are formally effects of UBs. As such, for the security crowd, it still makes sense to investigate, understand, and discuss the actual observed effects of UBs, especially why a compiler does this, what are the real-world actual variants of generated code (if any) for a given UB for this and other compilers, how can this be abused and exploited, and so on.<p>My point being - there are two sides to this coin.
    • bombcar1 hour ago
      I feel we need another category - <i>unspecified</i> behavior. I think everyone would agree the compiler should putout ONE of those answers and that nasal demons would be out of spec.<p>The problem is that it’s not specified which should be picked, but all pick something.
      • fc417fc8021 hour ago
        I agree what you say seems reasonable at a glance. But (IIUC) the issue is that for optimization we want the compiler to assume that UB doesn&#x27;t happen in order to constrain the possible code paths. So if it goes some distance down a possible execution branch and discovers UB it can trim the subtree. At that point &quot;anything can happen&quot; becomes an (approximate) reality.<p>The obvious counterpoint in this particular instance is that there&#x27;s no good reason not to make such an awful expression a compile time error.<p>I also personally think that evaluation order should be strictly defined. I&#x27;m unclear if the current arrangement ever offers noticable benefits but it is abundantly clear that it makes the language more difficult to reason about.
      • compiler-guy1 hour ago
        The C and C++ standards include &quot;Implementation defined behavior&quot;, which means that a conforming implementation can do whatever it wants, as long as it specifically documents and sticks to that behavior.<p>This doesn&#x27;t really help portability all that much.
  • compiler-guy2 hours ago
    With undefined behavior, a conforming compiler can do anything it wants at all, including generating a program that segfaults or something else.<p>But what often happens in practice is that &quot;Bill&#x27;s Fly-By-Night-C-Compiler-originally-written-in-the-mid-nineties&quot; implemented it in some specific way (probably by accident) and maintains it as a (probably informal) extension. And almost certainly has users who depend on it, and can&#x27;t migrate for a myriad of reasons. Anyway, it&#x27;s hard to sell an upgrade when users can&#x27;t just drop the new compiler in and go.<p>At the language level, it is undefined-behavior, and any code that relies on it is buggy at the language level, and non-portable.<p>Defining it would make those compiler non-conforming, instead of just dependent on defining something that is undefined.<p>Probably the best way forward is to make this an error, instead of defining it in some way. That way you don&#x27;t get silent changes in behavior.<p>Undefined behavior allows that to happen at the language level, but good implementations at least try not to break user code without warning.<p>Modern compilers with things like UBSan and such makes changing the result of undefined behavior much less of an issue. But most UB is also, &quot;No diagnostic required&quot;, so users don&#x27;t even know they have in their code without the modern tools.
  • Someone2 hours ago
    &gt; The interesting thing here is the Undefined Behavior (UB), well... actually two UBs, thanks to which there are three possible correct answers: 11, 12 and 13.<p>There’s UB, so any answer is possible, isn’t it?
    • gynvael5 minutes ago
      Hey! Author here :)<p>I&#x27;m going top-to-bottom through comments, and there was a similar question, so I&#x27;ll link my answer here: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=48140821">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=48140821</a> (TL;DR: you are right, but there&#x27;s another perspective on this)
  • p0w3n3d52 minutes ago
    On my CS lectures algorithms professor used this pseudo language when writing an algorithm on a whiteboard :<p><pre><code> I &lt;- I++ </code></pre> On the next hour another professor was giving lecture on C++ programming. I asked him the question: what would happen if we compiled<p><pre><code> i = i++ </code></pre> He went into some deep elaboration on it, but reassumed that only idiot would write like this...
  • adverbly23 minutes ago
    &#x2F;sarcastic<p>This is how to keep simpletons out of your code base. Every numeric constant is defined in terms of a different lang quiz. Works well in JS as well of course.<p><pre><code> const DEFAULT_SELECTION = true + true const BASE_PRICE = 4 * parseInt(0.0000001) const BILLING_DAY_OF_MONTH = a++ + ++a</code></pre>
  • HelloNurse5 hours ago
    The final value of a is that if you write this you are fired. It&#x27;s worse than a racist joke.
  • yason1 hour ago
    The only point you can conclude out of these discussions, especially in an interview, that it doesn&#x27;t matter what the answer happens to be on $CC and $ARCH but you wouldn&#x27;t want anyone to write stuff like that in the first place.<p>Failing to recognize the dangers would be an instant fail; knowing that something reeks of undefined behaviour, or even potential UB, is enough: you just write out explicitly what you want and skip the mind games.
  • tombert52 minutes ago
    I have always hated this crap; the fact that I&#x27;m not 100% sure the result of this indicates that maybe the ++ operator (pre or postfix) is something that should be avoided?<p>I don&#x27;t do a lot of C anymore, but even when I did, I always would do increments on separate lines, and I would do a +=1, or just a = a + 1. I never noticed a performance degradation, and I also don&#x27;t think my code was harder to read. In fact I think it was easier since I think the semantics were less ambiguous.
  • magicalhippo2 days ago
    Perhaps I&#x27;m just naive and&#x2F;or have forgotten too much C, not that I knew that much, but I&#x27;m a bit perplexed as to why this is UB.<p>It seems like something that should trigger a &quot;we should specify this&quot; reaction when adding these operators, and there is at least one reasonable way to define it which is fairly trivial and easily implementable.
  • vishnugupta2 hours ago
    I am, thankfully, out of this craziness now but it was fun solving ton of such puzzles from Yashavant Kanetkar books while preparing for campus hiring interviews back in 2000. &quot;Test Your C Skills&quot; in particular. Fun times.<p><a href="https:&#x2F;&#x2F;www.scribd.com&#x2F;document&#x2F;235004757&#x2F;Test-Your-C-Skills-Yashwant-Kanetkar-OCRd" rel="nofollow">https:&#x2F;&#x2F;www.scribd.com&#x2F;document&#x2F;235004757&#x2F;Test-Your-C-Skills...</a>
  • pplonski861 hour ago
    I love such puzzles! I used to use a lot ternary operators in C++ but one day friend of mine told me that I shouldn&#x27;t nest ternary operators too much because code is too complicated to read - he understands code perfectly, he was just worried about younger programmers. Since then I started to use longer versions of code instead of smart shortcuts - to improve readability of code.
  • nnm31 minutes ago
    ++ should be banned, just like goto
  • Tomte2 days ago
    Some C++ quiz with ++a and a++? It‘s always about sequence points, or better the lack of sequence points.<p>It‘s the standard technical C++ blog post everybody seems to write.
  • comrade12341 hour ago
    The a=13 was most surprising to me but in retrospect obvious and amusing.
  • syngrog6647 minutes ago
    The smart nerd will know precisely how to decode that line&#x27;s results.<p>The wise nerd will not allow lines like it in their codebase, in the first place and, having seen one, will refactor it (probably involving more lines or parentheses) to make it more clear and easier to maintain.<p>The latter approach scales better, in long run.
  • summarybot1 hour ago
    &gt; If you would like to test your compiler (posting back the results in the comments is really appreciated, especially from strange&#x2F;uncommon compilers and other languages which support pre- &#x2F; post- increment ....<p>Uh, 85% of them show the wrong result so 85% of them clearly do not support pre and post increment.
  • ecshafer2 hours ago
    hmm surprising. I assumed it would be 12 since 5+5+1+1 doesn&#x27;t really matter what order you do it in. But I suppose this really undefined behavior.
  • phendrenad22 hours ago
    Tried it on <a href="https:&#x2F;&#x2F;www.onlinegdb.com&#x2F;online_c_compiler" rel="nofollow">https:&#x2F;&#x2F;www.onlinegdb.com&#x2F;online_c_compiler</a> Returns 12. If I were designing C, it would return 13. But then again, I&#x27;m an assembly programmer.
    • topspin1 hour ago
      Godbolt: clang and gcc compilers give 12. msvc compilers yield 13.<p><pre><code> #include &lt;stdio.h&gt; int main() { int a = 5; a = a++ + ++a; printf(&quot;%d\n&quot;, a); return 0; } </code></pre> x64 msvc v19.50 VS18.2 output:<p><pre><code> example.c ASM generation compiler returned: 0 example.c Execution build compiler returned: 0 Program returned: 0 13 </code></pre> x86-64 gcc 16.1 output:<p><pre><code> ASM generation compiler returned: 0 Execution build compiler returned: 0 Program returned: 0 12 </code></pre> armv8-a clang 22.1.0 output:<p><pre><code> &lt;source&gt;:5:10: warning: multiple unsequenced modifications to &#x27;a&#x27; [-Wunsequenced] 5 | a = a++ + ++a; | ^ ~~ 1 warning generated. ASM generation compiler returned: 0 &lt;source&gt;:5:10: warning: multiple unsequenced modifications to &#x27;a&#x27; [-Wunsequenced] 5 | a = a++ + ++a; | ^ ~~ 1 warning generated. Execution build compiler returned: 0 Program returned: 0 12</code></pre>
  • onlyrealcuzzo1 hour ago
    Please tell me the answer is somehow 42!<p>int a = 5; a = (++a * a++) + --a; a = ?
  • nDRDY2 days ago
    Oh god. How long before yet another UB-based question ends up in technical coding interviews?
    • nananana92 hours ago
      The nice thing about these is that all answers are correct.
      • jbxntuehineoh2 minutes ago
        the correct answer is that the program will launch nethack, duh
  • nothrowaways2 hours ago
    12