Re:The C23 edition of Modern C is now available for free
by secondcoming on Tuesday October 15, 2024 @04:11PM
(#41852615)
Does C automatically wrap? I thought you need to pass `-fwrapv` to the compiler to ensure that.
[ Parent
| on HN ]
Re:The C23 edition of Modern C is now available for free
by greyw on Tuesday October 15, 2024 @04:36PM
(#41852833)
Unsigned overflow wraps. Signed overflow is undefined behavior.
Re:The C23 edition of Modern C is now available for free
by kbolino on Tuesday October 15, 2024 @04:45PM
(#41852909)
This distinction does not exist in K&R 2/e which documents ANSI C aka C89, but maybe it was added in a later version of the language (or didn't make it into the book)? According to K&R, all overflow is undefined.
Re:The C23 edition of Modern C is now available for free
by wahern on Tuesday October 15, 2024 @05:21PM
(#41853245)
I don't have my copy of K&R handy, but this distinction has existed since the initial codification. From C89:
3.1.2.5 Types
[...] A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.
Source: C89 (draft) at https://port70.net/~nsz/c/c89/c89-draft.txt
Re:The C23 edition of Modern C is now available for free
by renox on Tuesday October 15, 2024 @04:38PM
(#41852848)
-fwrapv is for signed integer overflow not unsigned.
Re:The C23 edition of Modern C is now available for free
by sp1rit on Tuesday October 15, 2024 @05:02PM
(#41853085)
Yes, as unsigned overflow is fine by default. AFAIK the issue was originally that there were still machines that used ones complement for describing negative integers instead of the now customary twos complement.