argc and argv
Reading program arguments.
Programs receive arguments
When you run a program from a terminal, you can pass it extra words called command-line arguments.
C delivers them to main through two parameters: argc (argument count) and argv (argument vector).
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("This program received %d arguments\n", argc);
return 0;
}The main signature
The full form of main is:
int main(int argc, char *argv[])
argc is the number of arguments. argv is an array of string pointers, one per argument.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("argc = %d\n", argc);
return 0;
}All lessons in this course
- argc and argv
- Parsing Options
- getopt
- Building a CLI Tool