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

后端 未结 13 2258
心在旅途
心在旅途 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 02:15

    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.

提交回复
热议问题