Passing argc
to main
seems to be redundant. Take a look at the following example.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[], char *envp[])
{
int j;
for (j = 0; j < argc; j++)
printf("argv[%d]: %s\n", j, argv[j]);
for (j = 0; envp[j] != NULL; j++)
printf("envp[%d]: %s\n", j, envp[j]);
for (j = 0; argv[j] != NULL; j++)
printf("argv[%d]: %s\n", j, argv[j]);
exit (EXIT_SUCCESS);
}
The first for
loop uses the standard way using argc
to enumerate the command line arguments.
The second loop shows how to enumerate the environment. The environment is stored in a list of pointers terminated by a NULL. The number of entries in the environment is not passed to main.
And the same applies to the list of arguments. The third loop shows how to enumerate the arguments without the use of argc
.
So the question is: why is argc
it passed to main
if it is redundant?