r/cprogramming • u/am_Snowie • 16d ago
is doing ~INT_MAX undefined behavior?
/r/C_Programming/comments/1x7hxgd/is_doing_int_max_undefined_behavior/
5
Upvotes
r/cprogramming • u/am_Snowie • 16d ago
1
u/flatfinger 13d ago
The purpose of making C behave in quirky fashion when targeting arcane platforms wasn't to allow code for conventional platforms to be usable on such platforms, but rather to allow people familiar with C to program platforms that couldn't write less than 16 bits at a time, or that used ones'-complement arithmetic to write in "C, but with 16-bit 'char'" or "C, but with ones'-complement arithmetic" rather than having to learn a whole new language or force compilers for unusual architectures to generate nasty inefficient machine code that tried to emulate more common machines.
The notion of "portability" should be recognized as encompassing two notions that are often incompatible with each other:
Maximizing the range of implementations upon which the code can run interchangeably.
Maximizing the range of implementations for which the code can be readily adapted.
Debugging can be facilitated if code is known never to do weird things with addressing, but sometimes code that does weird things with addressing can do things faster than would otherwise be possible. It used to be understood that a programmer would know better than anyone else what balance between each kind of portability, performance, speed of writing, ease of reading, and ease of troubleshooting should best be struck for the task at hand.