News for hackers. Stuff that matters.
Re:Almost Always Unsigned by whattheactualf3 on Sunday January 02, 2022 @07:40AM (#29768255)
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.
Re:Almost Always Unsigned by munch117 on Sunday January 02, 2022 @08:29AM (#29768546)
If you ignore the compiler warning. This is trivial for the compiler to detect.
Re:Almost Always Unsigned by jcelerier on Sunday January 02, 2022 @06:00PM (#29775042)
only in the most trivial cases

https://gcc.godbolt.org/z/YWPP5qdbx

Re:Almost Always Unsigned by captainmuon on Sunday January 02, 2022 @05:07PM (#29774511)
I 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).
Re:Almost Always Unsigned by virtualritz on Sunday January 02, 2022 @09:25AM (#29768901)
-Werror