4 ms·
Here's an example: unsigned int length_a = 10; unsigned int length_b = 20; if (length_a - length_b < 0) { printf("negative diff\n"); } else
by whattheactualf3 5y ago
Here's an example:
unsigned int length_a = 10;
unsigned int length_b = 20;
if (length_a - length_b < 0)
{
printf("negative diff\n");
}
else
{
printf("positive diff\n");
}
I would argue that there is plenty of code like that in the wild. It looks innocent and harmless but it gives a completely surprising result.
- munch117 5y agoIf you ignore the compiler warning. This is trivial for the compiler to detect.
- jcelerier 5y agoonly in the most trivial cases https://gcc.godbolt.org/z/YWPP5qdbx https://gcc.godbolt.org/z/YWPP5qdbx
- virtualritz 5y ago-Werror
- captainmuon 5y agoI don't understand why the programming language can't promote the temporary to a larger signed int. Of course you can't change it in C anymore, but its like this in most languages. But you rarely ever want unsigned modulo arithmetic here (and if you do it should be a special syntax).