I was practicing some Leetcode problems and I ran into this unexpected runtime error. I'm doing some signed integer operations and trying to set my input negative (i.e. 5 => -5 and -8 => -8)
However, in the case of a INT_MIN input when using a ternary operator, I get an integer overflow. Using an if statement, I don't.
x = x < 0 ? x : x * -1;
>>> runtime error: negation of -2147483648 cannot be represented in type 'int'
vs.
if (x > 0)
x *= -1;
>>> no problems with this one
Leetcode says "Compiled with gcc 8.2 using the gnu99 standard." for the language info. Is this expected behavior for all versions of C? My naive expectation was that a ternary operator was just syntactic sugar for normal control flow, but clearly I'm wrong on that