It seems that the argv[argc]
is always NULL
, so I think we can traverse the argument list without argc
. A single while
lo
We "need" it, because it's required by various standards.
We are free to ignore the value completely, but since it is first parameter of main
, we must have it in parameter list. In C++ (and probably non-standard C dialects), you can just omit the parameter name, like this C++ snippet (easy to convert to C):
#include // C-compatible include, guarantees puts in global namespace
// program will print contents of argv, one item per line, starting from argv[0]
int main(int /*argc*/, char *argv[]) { // uncomment argc for C
//(void)argc; // uncomment statement for C
for (int i=0; argv[i]; ++i) {
puts(argv[i]);
}
return 0;
}
In standard C, with common warnings settings, unused parameter generates warning, which can be fixed by a statement like (void)argc;
which causes the name to be used without generating any code.
argc
is nice to have, because otherwise many programs would need to walk thorugh the parameters to get the count. Also, in many programming languages with arrays that have length, there isn't any argc
parameter, there's just an array with the items.