Earlier quoted context omitted.
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'd likely see from other compilers, and why the standard might say that this sort of thing is UB. "What does this produce?" and expecting an answer of "17" is a bad question even if UB didn't mean the expected answer is wrong.
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.
Int a = 5; a = a++ + ++a; a =? (2011)
151–160 of 246 posts
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#152I 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. "Test Your C Skills" in particular. Fun times. https://www.scribd.com/document/235004757/Test-Your-C-Skills...
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#153Re: Int a = 5; a = a++ + ++a; a =? (2011)
#154I don’t have gcc available so I can’t test it, but I wonder what it does with int a = 5; int b = a++; if it gives b==5 in this circumstance (which I would say is the correct value), then it seems that giving 13 for a++ + ++a is a bug in the compiler. I kind of feel like giving 6 as an answer would also be a bug in the compiler since postfix-++ should return the old value and then increment.
int a = 5;
int b = a++;
has well defined behavior. The first line initializes a to 5. The second initializes b to 5 and sets a to 6. (The language doesn't specify the order of the two operations of assigning a value to be and incrementing a, but in this case it doesn't matter.)Giving 13 for a++ + ++a is not a bug in the compiler. It's a bug in the code.
The correct answer to "what does a++ + ++a do" is "it gets rejected in code review and replaced with code that expresses the actual intent.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#155++ should be banned, just like goto
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#156> If you would like to test your compiler (posting back the results in the comments is really appreciated, especially from strange/uncommon compilers and other languages which support pre- / post- increment .... Uh, 85% of them show the wrong result so 85% of them clearly do not support pre and post increment.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#157Earlier quoted context omitted.
Not nasal demons in this case ( https://groups.google.com/g/comp.std.c/c/ycpVKxTZkgw/m/S2hHd... ): thaumasiotes shows that we can expect a numeric answer.
I don't see the name "thaumasiotes" at that link, nor do I see anything relevant to the code in the title. The behavior of "int a = 5; a = a++ + ++a;" is undefined. There is no guarantee of a numeric result, because there is no guarantee of anything .
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#158Earlier quoted context omitted.
Applying the increment or decrement operators over the same variable more than once on the same line should be a compile-time error. Anyway, yes, this one example has an obvious order it should be applied. But still, something like it shouldn't be allowed.
Honestly, having increment in expressions rather than a statement feels like more of a footgun than a benefit. Expressions shouldn't mutate things.
I think if anything people have been leaning more and more into expressions over statements, because when everything is an expression you end up being able to walk the gradient of complexity a bit more nicely than when you end up with a thing that just has to be broken down to a bunch of statements.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#159Earlier quoted context omitted.
> It's Undefined Behavior. Susam's post doesn't make this clear. The quotes from K&R say that the modifications to the variable may take place in any order, but they don't directly say that doing this is Undefined Behavior, which would make it permissible to do anything, including e.g. interpreting the increments as decrements. The C99 standard is quoted saying this: >> Between the previous and next sequence point an…
The "shall" in the standard means it's undefined behavior. This is explained in the "Conformance" section, > 2. If a ‘‘shall’’ or ‘‘shall not’’requirement that appears outside of a constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no differen…
Yes, I see that. I just said they should refuse.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#160Earlier quoted context omitted.
I don't see the name "thaumasiotes" at that link, nor do I see anything relevant to the code in the title. The behavior of "int a = 5; a = a++ + ++a;" is undefined. There is no guarantee of a numeric result, because there is no guarantee of anything .
It's only the order of evaluation that is undefined.