r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
Blog post Unsigned Sizes: A Five Year Mistake
https://c3-lang.org/blog/unsigned-sizes-a-five-year-mistake/
91
Upvotes
r/ProgrammingLanguages • u/Nuoji C3 - http://c3-lang.org • May 03 '26
View all comments
Show parent comment
1
u/Tasty_Replacement_29 Bau May 06 '26
> You can iterate backwards like this
Yes, you can. You can. But can all the others? With signed integers, things like that are much less of an issue. James Gosling (designer of Java): "[...] For me as a language designer, [...], what "simple" really ended up meaning was could I expect J. Random Developer to hold the spec in his head. [...] Quiz any C developer about unsigned, and pretty soon you discover that almost no C developers actually understand what goes on with unsigned, what unsigned arithmetic is. Things like that made C complex."
> Again, type system issue and count (and sizes) should be unsigned in the first place.
Maybe you are not aware of this issue then. So you have code like this (printf for illustration):
That works, any maybe you get:
But now if you try this:
You get something like this:
So the pointer is still valid, it just happens to allocate zero bytes. So you can't use the return value to check whether it worked.
Right. The problem now is: unsigned is supported, and so now many developers use unsigned, return unsigned in their library etc. And then you have to do operations on them (add, multiply etc.). If the default type is signed (via type inference), how do you do that? Do you cast the signed to unsigned, or the unsigned to signed? It's more work, you have to think about it, and you have two choices. If you don't have unsigned, then the problem goes away.
I see that now, but no it was not clear to me... I thought more people know about the problems of unsigned...
In theory you are right. In practice, C/C++ compilers did the obvious thing, because it's the easiest and fastest. As for the standard, with C++11 this is mostly fixed ("signed integer type with width of exactly 8, 16, 32 and 64 bits respectively with no padding bits and using 2's complement for negative values (provided only if the implementation directly supports the type").
But we don't need to argue about the mistakes of other languages :-)
I do think that maybe "byte" in my language should be unsigned, to avoid having to type "& 0xff" each time. I don't think this should be a problem.