Command-Line Arguments
Prerequisites: File I/O
main can receive the program's command-line arguments directly:
int main(int argc, char *argv[]) {
printf("Program name: %s\n", argv[0]);
printf("Argument count: %d\n", argc);
for (int i = 1; i < argc; i++) {
printf("Arg %d: %s\n", i, argv[i]);
}
return 0;
}
./myprogram hello world
Program name: ./myprogram
Argument count: 3
Arg 1: hello
Arg 2: world
argc counts the arguments (including the program name itself at
argv[0]); argv is an array of char * strings.