What'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…
The short answer is because C was designed to give leeway to really dumb compilers on really diverse hardware. This isn't quite the same case, but it'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…
Int a = 5; a = a++ + ++a; a =? (2011)
61–70 of 246 posts
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#62Earlier 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.
Having an understanding of how the code gets transformed into machine code helps. For this case, there'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't affect the outcome no matter how it's done. In `a++ + ++b` the interleaving can affect the outcome, and that's your sign that something might be wrong.
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.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#63What'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…
I didn't open TFA but my first thought was "Is this even defined?".
It kinda make sense that suck fucktardedness could be not defined.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#64Earlier 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 That would be nice, but don't forget the more general case of pointers and aliasing: int a = 5; int *pa = &a; printf("%d", (a++ + ++*pa)); The compiler cannot statically catch every possible instance of a statement where a variable is updated more than once.
Well, aliased updates are undefined behavior already.
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#65Re: Int a = 5; a = a++ + ++a; a =? (2011)
#66This 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.
const DEFAULT_SELECTION = true + true
const BASE_PRICE = 4 * parseInt(0.0000001)
const BILLING_DAY_OF_MONTH = a++ + ++aRe: Int a = 5; a = a++ + ++a; a =? (2011)
#67The 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…
Do you want a job at a place where someone who doesn't understand UB makes the hiring decisions?
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#68The 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…
Do you want a job at a place where someone who doesn't understand UB makes the hiring decisions?
Re: Int a = 5; a = a++ + ++a; a =? (2011)
#69The statement is valid C#, which has left-to-right execution order and no undefined behavior. The answer is 5 + 7 = 12.
awk 'BEGIN{a=5; a = a++ + ++a; print a}'
12Re: Int a = 5; a = a++ + ++a; a =? (2011)
#70The 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…
Other than the job for most programmers having nothing to do with whether they know the outcome, because hopefully they'd never write something like it or clean it up. And IF they found it they'd hopefully test it - given that it appears to be compiler dependent anyways.