65

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

I've always just ignored it when trying rid of all my signed unsigned comparison warnings, but never understood why it is the way that it is.

0

13 Answers 13

58

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

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

rather than

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

Edit: effectively, as Aaron reminded us, the very original syntax would have been something like

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

Since the "prototypes" were only introduced later. That came roughly after everyone had logged a minimum of at least 10 hours chasing subtle (and not so subtle) type-related bugs

3
  • 16
    Actually it would have been `main(argc,argv) char* argv { } as inline parameter declaration and prototypes weren't introduced until ANSI C (ahh - the good old days... I'm so glad they're gone)
    – Aaron
    Nov 20, 2009 at 23:58
  • 5
    @right, Aaron! I forgot... We'd better stop before we show our age too much ;-)
    – mjv
    Nov 21, 2009 at 0:07
  • 1
    @Aaron Had not seen main(argc, argv) char **argv {... } old style function definition, but have seen/used main(argc, argv) int argc; char* argv; { ... } Jul 29, 2015 at 20:56
33

A few reasons:

  • because it doesn't matter
  • because C did not originally have the unsigned keyword or unsigned integer types
  • because C did not originally check parameter types and did not even have prototypes.
    As a result, it was common practice to not even declare int types, as this was the default.
  • because int was, in a sense, more important back then. Everything was an int. C evolved in part from a language that did not even have types. Every single varable was a word, which is what int originally was used for.

UPDATE: Jason S asked for sources. I think you can dig all of this (except for "it doesn't matter") out of a paper by dmr which is on line: The Development of the C Language. You may need to look up the earlier languages BCPL and B in the usual places.

0
12

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

3
  • 9
    C is old. It was designed that way from the start. But I have to disagree with that last sentence (not enough to downvote though). ISO could quite easily add a third prototype definition for main with little impact. In fact, implementors are also free to add other main prototypes - it's explicitly mentioned in the standard.
    – paxdiablo
    Nov 21, 2009 at 0:18
  • 3
    @paxdiablo agreed, standards can and should be evolved and changed [with care]. In this particular case, however, how does this unsigned argc thing helps with world peace?
    – mjv
    Nov 21, 2009 at 0:34
  • 1
    @mjv Less people griping about things-that-aren't-exactly-correct on the Internet means less people griping in general, which means there will be slightly more world-peace than before.
    – Dai
    May 19, 2017 at 21:54
12

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.

3
  • 1
    You beat me to it. Yea, I don't believe B (predecessor of C) had anything but char and int and every function returned an int
    – Earlz
    Nov 21, 2009 at 0:03
  • 2
    B didn't have char. Nor int for that matter. It was untyped. char==int==void*
    – user14554
    Nov 21, 2009 at 3:16
  • Link broken again. Sep 15, 2022 at 14:51
8

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.

3
  • That's due to the fact that C++ lacks a proper for_each construct. I ran in the same problem multiple times, but I'm always glad if I can use the std containers with proper iterators.
    – xtofl
    Nov 21, 2009 at 6:44
  • agreed, but in C loops is all you have Nov 21, 2009 at 6:56
  • You should rewrite it as for (size_t i = SIZE - 1; i < SIZE; i--) and voila. ;) Source: <gustedt.wordpress.com/2013/07/15/…> Sep 15, 2022 at 14:52
7

The Google C++ Style Guide suggests never to use unsigned int types unless you're working with actual bit patterns. Their rationale applies to C as well. Quick summary line:

... C's type-promotion scheme causes unsigned types to behave differently than one might expect. ... Don't use an unsigned type.

This was probably not in the minds of the original creator's of C, but who knows‽

6
  • 4
    I don't understand Google's claim that unsigned types behave "unexpectedly" (which in context must mean, more unexpectedly than signed types). Signed and unsigned types misbehave when used together, one will be promoted to the signedness of the other. So, the type promotion system causes all integer types to "behave different than one might expect". Nov 21, 2009 at 18:45
  • ditto. wtf? bits are bits until you do plain arithmetic on them, at which point unsigned and signed each are perfectly valid for particular applications.
    – Jason S
    Nov 22, 2009 at 6:24
  • One thing that's nice about signed is that you always have a way to represent an invalid value. You do have to waste an entire bit on it though.
    – Adam Goode
    Nov 22, 2009 at 15:24
  • -1U is an equally-useful "invalid value" in most contexts. For instance nothing could actually have size equal to the entire size of address space, minus 1. Actually I like Jason's comment. One change I wish could be made to C is eliminating signed/unsigned types and replacing them with signed/unsigned operator variants. But alas... Aug 1, 2010 at 18:59
  • That document is nonsense. Read MISRA or CERT if you want serious coding guidelines written by professionals.
    – Lundin
    Dec 14, 2016 at 15:11
5

As a solution to your warning problem, you could do something like this to suppress the warnings:

const unsigned int uargc = (unsigned int) argc;
1
  • 4
    Right, since the question was tagged c I thought I'd use what would work in both. Nov 21, 2009 at 1:10
4

It was a prescient design decision to make it easier to port C programs to Java in the future since there are no unsigned types in Java.

2
  • 6
    You must not know c programmers very well. If they had prescience, they surely would have made sure to make porting to Java more difficult rather than less ;)
    – Catskul
    Nov 21, 2009 at 0:56
  • @Rob: ??? I wouldn't call it "easier" -- it's only easier for people doing signed arithmetic. For handling unsigned arithmetic and bitstrings Java is a real pain.
    – Jason S
    Nov 22, 2009 at 6:27
3

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.

1

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?

8
  • 2
    C could have named them int and uint, then it is only one extra character.
    – Zan Lynx
    Nov 20, 2009 at 23:51
  • Great answer ;) like millions I think
    – Elalfer
    Nov 20, 2009 at 23:55
  • 2
    @Zan: I have always wished this were so
    – Catskul
    Nov 20, 2009 at 23:56
  • 8
    it's a little known secret that most verbose languages are sponsored by the keyboard companies.
    – cobbal
    Nov 21, 2009 at 0:19
  • 3
    I believe COBOL was supported by the Amalgamated Union of Caps Lock and Shift Key Designers and Craftsmen.
    – caf
    Nov 21, 2009 at 0:56
1

By setting it to int, the range is limited to between 1 and INT_MAX inclusive. This will typically mean that no accidental cast or alias will take it out of range from an inadvertent wrap around. It also allows implementations to use the entire negative and 0 range for system specific scenarios.

Ok, I just made that up. The real reason is that it was just an arbitrary decision that one of the original C language developers made, and nobody really thought that hard about it until now. :)

-1

Just a simple question: Do you expect more than 231 (or even more than 215) command line arguments? I doubt that most operating systems could handle that many.

4
  • 1
    int though must be at least 2^15-1 Nov 21, 2009 at 0:22
  • 2
    thats not why I was asking, but definitely possible rm $(find / -mindepth 3)
    – Catskul
    Nov 21, 2009 at 1:07
  • The find -exec option command provides rm with exactly one argument at a time. The previous rm command might fail on systems that have an upper limit on the number (or total length) of a command line. Nov 23, 2009 at 2:51
  • @paxdiablo: false. C guarantees INT_MAX is at least 32767. Aug 1, 2010 at 19:00
-2

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.

4
  • 9
    "C times" are hardly over. I for one won't stop using C anytime soon. Nov 21, 2009 at 6:46
  • The times when C ruled are over, and that's what I meant.
    – quant_dev
    Nov 21, 2009 at 19:44
  • Except C is completely dominant in embedded systems and will remain as such until something better is invented. The C++11 and C++14 revisions have ensured that C++ has no future in embedded systems, if anywhere.
    – Lundin
    Dec 14, 2016 at 15:06
  • In what way have C++11 and C++14 ensured this? All we really need for C++ Std Libray to be useable in embedded systems is real-time safe exception handling. Ada has been doing it since the late 80's, so it's definitely possible. Someone just needs to safety critical certify a version of the STL.
    – Vinny
    Aug 11, 2022 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.