6.3 Command-line Arguments
A program can receive arguments from the command line. argc is the argument count, and argv stores those arguments as strings.
c
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s file.txt\n", argv[0]);
return 1;
}
printf("processing %s\n", argv[1]);
return 0;
}argv[0] is usually the program name, argv[1] is the first user argument, and so on. Every argument is a string, so convert them with atoi() or strtol() when you need numbers.
c
int times = atoi(argv[1]); // "3" -> 3Write error messages to stderr and normal results to stdout. That way users can redirect normal output to a file while still seeing errors in the terminal.
bash
./greet Ada > out.txtLoading interactive lab...
Loading concept check...
Loading practice...