Why is argc "int" (and not "unsigned int")?

Why do command line arguments count the variable (traditionally "argc") "int" instead of "unsigned int"? Is there a technical reason for this?

I always simply ignored it, trying to get rid of all my signed unsigned comparison warnings, but I never understood why it is.

+58
c ++ c command-line
Nov 20 '09 at 23:40
source share
13 answers

The fact that the C source language was such that, by default, any variable or argument was defined as an int , is probably another factor. In other words, you could:

main(argc, char* argv[]); /* see remark below... */ 

but not

 int main(int argc, char *argv[]); 

Edit : effectively, as Aaron reminded us, a very original syntax would be something like

  main(argc, argv) char **argv {... } 

Since the "prototypes" were introduced later. This happened approximately after everyone recorded at least 10 hours at a time, pursuing subtle (and not very subtle) type errors

+48
Nov 20 '09 at 23:49
source share

A few reasons:

  • because it does not matter.
  • because C initially didn't have the unsigned keyword or unsigned integers
  • because C did not initially check parameter types and did not even have prototypes. As a result, it was common practice to not even declare int types, since that was the default.
  • because int was in a sense more important then. Everything was int. C partially evolved from a language that didn't even have types. Each individual variable was word , for which int originally used.

UPDATE: Jason S. asked for sources. I think you can dig it all (except “it doesn't matter”) from the dmr article, which is online: Developing the C language . You may need to look for earlier BCPL and B languages ​​in regular places.

+30
Nov 21 '09 at 0:51
source share

Because C is old, and it was designed that way from the start. It's too late to change it now.

+12
Nov 20 '09 at 23:44
source share

Here is the history of the C programming language in dmr's own words. It was not explicitly specified (at least not from what I gave it), but the earliest versions of C did not support unsigned types. The mjv point about implicit typing on int also matters.

EDIT

The Bell Labs link has been broken for a while: here is an alternative link to the same paper.

+11
Nov 20 '09 at 23:58
source share

Another reason may be that unsigned types may be inconvenient for iteration. For example, this fragment iterates down:

 for (size_t i = SIZE - 1; i >= 0; --i) ... 

This is essentially a mistake. When I reach 0 in the last iteration, it will go directly to 4294967295 (on a 32-bit machine), and the cycle will not end.

For this reason, I personally find simple ints more convenient for iteration. You do not have to be especially careful when switching the for loop from counting to counting when using int.

+7
Nov 21 '09 at 6:37
source share

The Google C ++ Style Guide suggests never using unsigned int types unless you work with actual bit patterns. Their rationale applies to C. Short summary line:

... The C-type promotion scheme causes unsigned types to behave differently than might be expected .... Do not use an unsigned type.

It probably wasn't in the minds of the original author of C, but who knows

+6
Nov 21 '09 at 1:06
source share

As a solution to the warning problem, you can do something similar to suppress warnings:

 const unsigned int uargc = (unsigned int) argc; 
+5
Nov 20 '09 at 23:46
source share

This was a preliminary design decision to simplify porting C programs to Java in the future, since there are no unsigned types in Java.

+4
Nov 21 '09 at 0:37
source share

The declaration for main() was defined before unsigned types were added to the language - see the DMR page on the Initial C page. Too late to change when unsigned was added.

+3
Nov 22 '09 at 18:59
source share

I see how strange this may seem: argc should not be negative! But look at it this way: both int and unsigned int cover a range of values ​​that you accept (if you have 2 ^ 31 command line options, you have a problem), and int shorter for input.

Ask a puzzle question: how many keyboards would be used by typing unsigned if C left unsigned int argc ?

+1
Nov 20 '09 at 23:47
source share

By setting it to int, the range is limited between 1 and INT_MAX inclusive. This usually means that no random casts or aliases will go beyond unintentional wrapping. It also allows implementations to use all negative and range 0 for system-specific scenarios.

Ok, I just did it. The real reason is that it was just an arbitrary decision made by one of the C language developers, and so far no one has thought about it. :)

0
Nov 20 '09 at 23:56
source share

A simple question: do you expect more than two command line arguments 31 (or even more than 2 15 )? I doubt most operating systems can handle this.

0
Nov 21 '09 at 0:07
source share

I assume it is designed to be C compatible, and C times people didn’t really care about the validity of signed / unsigned ones.

-one
Nov 20 '09 at 23:43
source share



All Articles