The answer to the main question was asked several times: the unsigned
keyword can only be used as a type specifier for an integral type.
As to why unsigned
is a separate keyword and not a uint
keyword, the reasons for this are historical.
The earliest versions of C (pre-K & R) had only four main types:
char
(8 bits, signed, 2'-complement)int
(16 bits, signed, 2'-pad)float
(32 bit)double
(64 bits, same range as float
, but greater accuracy)
Note that it is missing: no signed
or unsigned
keywords, no short
, long
or long double
; they were all added later. (Programmers who need unsigned arithmetic commonly used pointers that were freely interchangeable with int
.)
Each fundamental type had a name, which was one keyword, which made grammar simple.
When other types were added later, it made sense to add qualifiers such as unsigned
, short
and long
to existing type names rather than entering new keywords (which could break existing code). When the ANSI C committee standardized this language in 1989, they had to make a coherent structure from existing not quite formal definitions, while remaining in line with existing implementations. The result is what we have now, where int long unsigned long
is a valid type name (most often written as unsigned long long
).
If the language was developed from scratch, I suspect that a different approach would be used. Perhaps for each fundamental type there will be one keyword (for example, the C # approach) or, perhaps, fundamental type names will use a more consistent scheme, rather than a mess of keywords (for example, int:2
for 2 -byte integer, unsigned:4
for a 4-byte unsigned integer). But C and C ++ are stuck in the current approach.
Link: http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf
source share