Why is the command line arguments count variable (traditionally argc
) an int
instead of an unsigned int
? Is there a technical reason for t
The declaration for main()
was defined before the unsigned types were added to the language - see DMR's page on 'Primeval C'. It was too late to change when unsigned was added.
I suppose it is designed to be compatible with C, and in C times people didn't really care that much about signed/unsigned correctness.
Here's a history of the C programming language in dmr's own words. It's not stated explicitly (at least not from the quick skim I gave it), but the earliest versions of C didn't support unsigned types. mjv's point about implicit typing to int
is also relevant.
EDIT
The Bell Labs link has been broken for a while: here's an alternate link to the same paper.
Another reason could be that unsigned types can be inconvenient for iteration. For example, this snippet iterating down:
for (size_t i = SIZE - 1; i >= 0; --i)
...
Is, in fact, a bug. When i reaches 0 in the last iteration, it will go on right into 4294967295 (on a 32-bit machine) and the loop won't terminate.
For this reason, I personally find plain ints more convenient for iteration. You don't have to be especially careful when you switch a for
loop from counting up to counting down when using ints.
As a solution to your warning problem, you could do something like this to suppress the warnings:
const unsigned int uargc = (unsigned int) argc;
I see how it might seem weird: argc
should not be negative! But look at it this way: both int
and unsigned int
cover the range of values you'd accept (if you have 2^31 command line parameters, you have a problem) and int
is shorter to type.
Interview puzzle question: how many keyboards would have been used up typing the unsigned
if C had gone with unsigned int argc
?