Int a = 5; a = a++ + ++a; a =? (2011)
gynvael.coldwind.pl
Int a = 5; a = a++ + ++a; a =? (2011)
1–10 of 246 posts
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#2Re: Int a = 5; a = a++ + ++a; a =? (2011)
#3It‘s the standard technical C++ blog post everybody seems to write.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#4It seems like something that should trigger a "we should specify this" reaction when adding these operators, and there is at least one reasonable way to define it which is fairly trivial and easily implementable.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#5 int a = 5;
a += a++ + a++;
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 'undefined behaviour' or 'sequence points'.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.
I am very glad these types of interview questions have become less prevalent these days. They have, right? Right?
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#6Re: Int a = 5; a = a++ + ++a; a =? (2011)
#7The 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 'attracts' its ill-defined signed integers when you'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)
But this here is something that could be completely defined at the language level, there'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/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).
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#8The code in the post seems very similar to the one in my own post from 2010: https://susam.net/sequence-points.html int a = 5; a += a++ + a++; 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 co…
I just refuse to do interviews like that any more.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#9What's the reason that C didn't define the order of this? 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 'attracts' its ill-defined signed integers when you're using unsigned ones by returning a signed int when left shifting an uint16_t by an uint16_t for example is not as…
Anyway, yes, this one example has an obvious order it should be applied. But still, something like it shouldn't be allowed.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#10https://www.scribd.com/document/235004757/Test-Your-C-Skills...