News for hackers. Stuff that matters.
Re:Using signed or unsigned int by default in C (2015) by reltuk on Tuesday August 23, 2022 @01:21PM (#32568075)
Similar arguments are also seen in https://graphitemaster.github.io/aau/.

When using unsigned ints for backward iteration, I'm partial to looping as:

  for (i = size - 1; i < size; i--) {
    ...
  }
since it has so much symmetry with forward iteration.
Re:Using signed or unsigned int by default in C (2015) by 10000truths on Tuesday August 23, 2022 @01:27PM (#32568172)
I would rather just use a plain for loop and manipulate the index in the loop body:

  for(uint32_t i = 0; i < length; ++i) {
      i = (length - 1) - i;
      // ...
  }
Much easier for me to understand, making it less likely to mess anything up.
Re:Using signed or unsigned int by default in C (2015) by edflsafoiewq on Tuesday August 23, 2022 @01:32PM (#32568239)
You'll need a new variable there, but otherwise yeah.
Re:Using signed or unsigned int by default in C (2015) by WalterBright on Tuesday August 23, 2022 @03:07PM (#32569725)
Even easier (for D):

    foreach (i; 0 .. length)
D takes care of selecting the correct type for `i`.
Re:Using signed or unsigned int by default in C (2015) by phkahler on Tuesday August 23, 2022 @01:38PM (#32568319)
I'd argue that is a terrible way to write it since it depends on the rollover to terminate. Using i >= 0 is less mental work but also requires that i be signed.
Re:Using signed or unsigned int by default in C (2015) by ghoward on Tuesday August 23, 2022 @01:40PM (#32568370)
Rollover on unsigned ints is well-defined in C.
Re:Using signed or unsigned int by default in C (2015) by hbn on Tuesday August 23, 2022 @03:36PM (#32570240)
I think their point is that it's less well defined in the average human's brain

When most people see 0 - 1 they don't immediately think 11111111111111111111111111111111

Re:Using signed or unsigned int by default in C (2015) by ghoward on Tuesday August 23, 2022 @05:09PM (#32571551)
We're talking about C, though. I hope the average human is not writing C.
Re:Using signed or unsigned int by default in C (2015) by jcelerier on Wednesday August 24, 2022 @04:17AM (#32576313)
the average person writing C is absolutely an average human in terms of relationship with mathematics
Re:Using signed or unsigned int by default in C (2015) by xdavidliu on Tuesday August 23, 2022 @01:48PM (#32568484)
if we used i >= 0 and someone were to come in and change "int i" to "unsigned i" or "size_t i", the result would be catastrophic
Re:Using signed or unsigned int by default in C (2015) by Leherenn on Tuesday August 23, 2022 @03:07PM (#32569739)
The same argument can be applied to GP's code with the added bonus it will cause UB at some point.
Re:Using signed or unsigned int by default in C (2015) by JonChesterfield on Tuesday August 23, 2022 @08:58PM (#32573722)
`for (unsigned i = n; i-->0;) {}`

Probably formatted i-- > 0. Concise and reads (once used to it anyway) something like i decrementing to zero.