Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 362
Catskul Avatar asked Nov 20 '09 23:11

Catskul


People also ask

Why is argc an int?

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.

Why do we need argc?

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

What does Argy and argc indicate in int main int Arge char * argv?

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.

Is argc always at least 1?

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.


1 Answers

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

like image 175
mjv Avatar answered Sep 21 '22 00:09

mjv