Sunday, February 21, 2016

What is the argc variable in C ?

The argc variable indicates how many arguments were passed in to a C program when invoked through the command line (a terminal or shell). Here's an example of how it works.
#include <stdlib.h>
#include <stdio.h>
int
main(int argc, char **argv)
{
    printf("argc is %d\n", argc);
    return (0);
}
Running this little example program with a few arguments gives us:
$ ./example
argc is 1
$ ./example foo
argc is 2
$ ./example foo bar
argc is 3
The path to the application is always passed in to the program by the shell, so that's the "1" in the first example.

No comments:

Post a Comment