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.
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.
Providing argc therefore isn't vital but is still useful. Amongst other things, it allows for quick checking that the correct number of arguments has been passed. 2 ... argc shall be the number of arguments passed to the program from the environment in which the program is run. ....
Correct Option: C The name of the variable argc stands for "argument count"; argc contains the number of arguments passed to the program. The name of the variable argv stands for "argument vector". A vector is a one-dimensional array, and argv is a one-dimensional array of strings.
c. As you can see, the first argument ( argv[0] ) is the name by which the program was called, in this case gcc . Thus, there will always be at least one argument to a program, and argc will always be at least 1.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With