Why is argc an 'int' (rather than an 'unsigned int')?

后端 未结 13 2235
心在旅途
心在旅途 2021-01-31 01:37

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

相关标签:
13条回答
  • 2021-01-31 01:51

    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.

    0 讨论(0)
  • 2021-01-31 01:51

    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. :)

    0 讨论(0)
  • 2021-01-31 01:52

    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 讨论(0)
  • 2021-01-31 01:52

    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‽

    0 讨论(0)
  • 2021-01-31 02:01

    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.

    0 讨论(0)
  • 2021-01-31 02:04

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

    0 讨论(0)
提交回复
热议问题