Program: cmdargs.c
/* Program to read and display the command-line arguments */ #include <stdio.h> int main(int argc, char *argv[]) /* argc - No. of Command-line Arguments passed to the program */ /* argv - Array of Strings - Command-line Arguments */ { int i; printf("No. of Command-line Arguments : %d\n", argc); for (i = 0; i < argc; i++) { printf("Arg[%d] = %s\n", i, argv[i]); } return 0; } |
Output:
$ ./cmdargs one two three No. of Command-line Arguments : 4 Arg[0] = ./cmdargs Arg[1] = one Arg[2] = two Arg[3] = three $ |
As you can see the "argc" contains the number of command-line arguments passed to the program.
The very first command-line argument for the program is program name itself, followed by the other arguments passed.
No comments:
Post a Comment