News for hackers. Stuff that matters.
Re:Parameterized types in C using the new tag compatibility ... by uecker on Friday June 27, 2025 @01:53PM (#44398828)
"Naturally, sizes should be unsigned because they represent values which cannot be unsigned."

Unsigned types in C have modular arithmetic, I think they should be used exclusively when this is needed, or maybe if you absolutely need the full range.

Re:Parameterized types in C using the new tag compatibility ... by enqk on Friday June 27, 2025 @03:30PM (#44399566)
yeah unsigned are really about opting to perform modular arithmetic, or for mapping hardware registers.

C is weakly typed, the basic types are really not to maintain invariants or detect their violation

Re:Parameterized types in C using the new tag compatibility ... by AlotOfReading on Friday June 27, 2025 @09:23PM (#44401677)
If you need saturating arithmetic, C23 adds that as well with a new header: https://en.cppreference.com/w/c/header/stdckdint
Re:Parameterized types in C using the new tag compatibility ... by TuxSH on Friday June 27, 2025 @08:59PM (#44401593)
So do signed types (guaranteed to be 2's complement since C23, though all sane targets have been using that representation for a long time).

Two's complement encodes -x as ~x + 1 = 2^n - x = -x (mod 2^n) and can therefore be mixed with unsigned for (+, -, *, &, |, ^, ~, <<).

> I think they should be used exclusively when this is needed

The opposite: signed type usage should be kept to a minimum because signed type (and pointer) overflow is UB and will get optimized as such.

Re:Parameterized types in C using the new tag compatibility ... by uecker on Saturday June 28, 2025 @01:53AM (#44402548)
For signed types you can can also get a run-time trap on overflow, making it safe to use. Bug caused by unsigned wraparound are extremely hard to find.