4 ms·
I agree that signed is generally inferior and less often what you really need. For me, in C and C++, the main arguments for 'unsigned by default' are: (1) int
by beeforpork 5y ago
I agree that signed is generally inferior and less often what you really need. For me, in C and C++, the main arguments for 'unsigned by default' are:
(1) integer overflow/underflow is undefined behaviour. You don't want that, it's evil. Unsigned does not have that problem (often it is still a bug if you over- or underflow -- but at least you get sane code from the compiler).
(2) size_t is unsigned and is returned by sizeof() and is the argument to memcpy(), strcpy(), etc., and with -Wconversion, you do not want to handle all the warnings when mixing this with signed loop counters or size variables.
Unfortunately, one problem with using unsigned by default in C is that ptr1-ptr2 yields ptrdiff_t, which is signed. Which is bad, in my opinion, because I usually want to find an array size or index when I subtract two pointers, so I want size_t, and in these cases, I always know which pointer is larger.
Oh well, it's C.