News for hackers. Stuff that matters.
Re:Integers in C quiz by jcranmer on Sunday September 04, 2022 @11:06AM (#32713849)
The answer of the final question (INT_MIN % -1) is wrong in a way that's somewhat dangerous.

If you read the text of C99 carefully, yes, it's implied that INT_MIN % -1 should be well-defined to be 0. However, the % operator is usually implemented in hardware as part of the same instruction that does division, which means that on hardware where INT_MIN / -1 traps (thereby causing undefined behavior), INT_MIN % -1 will also trap. The wording was changed in C11 (and C++11) to make INT_MIN % -1 explicitly undefined behavior, and given the reasoning for why the wording was changed, users should expect that it retains its undefined behavior even in C89 and C99 modes, even on 20-year-old compilers that predate C11.

Re:Integers in C quiz by greaterthan3 on Sunday September 04, 2022 @06:19PM (#32718279)
>The wording was changed in C11

And here's an exact quote from the C11 standard:

>If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a; otherwise, the behavior of both a/b and a%b is undefined.

http://port70.net/~nsz/c/c11/n1570.html#6.5.5p6