4 ms·
You are correct about every published version of the C standard. C-next plans to standardized on 2's complement for signed integers, for what it's worth.[0] (
by loeg 9y ago
You are correct about every published version of the C standard. C-next plans to standardized on 2's complement for signed integers, for what it's worth.[0]
(For developers: additionally, as a non-standard feature, most popular open source compilers (i.e., gcc and clang) support an option called "-fwrapv", which does not treat 2's complement overflow as Undefined Behavior. This is useful for getting a large legacy codebase working on newer compilers, if it makes assumptions about 2's complement. Until the new C standard is published it's probably best to avoid relying on 2's complement overflow in new code.)
[0]: https://twitter.com/jfbastien/status/989242576598327296 https://twitter.com/jfbastien/status/989242576598327296
- moefh 9y agoSigned overflow will still be undefined behavior even after the planned change (making signed integers 2's complement). This is explicitly mentioned in the page linked in that tweet: > Status-quo If a signed operation would naturally produce a value that is not within the range of the result type, the behavior is undefined. The author had hoped to make this well-defined as wrapping (the operations produce the same value bits as for the corresponding unsigned type), but WG21 had strong resistance against this.