In your program, it is assumed that the first argument is the name of the file to be opened or if it failed to print an error message.
argc, argv [] - This is how the OS sends call parameters to the program.
For example, you executed the gcc -o myprog tc command. In tc, a program with main (int ac, char * av []) in C.
#include <stdio.h> #include <stdlib.h> main (int ac, char *av[]) { int i; for (i = 0; i < ac; i++) printf ("arg[%d] = '%s'\n",i,av[i]); exit(0); }
The compiler will create a boot module (executable file ... well, or as it is called in textbooks ???) with the name myprog (in Windows myprog.exe) in the current table of contents.
Perform it by typing the command ./myprog a1 a2 a3 (on Windows myprog a1 a2 a3) the program will print:
arg[0] = 'myprog' arg[1] = 'a1' arg[2] = 'a2' arg[3] = 'a3'
Those. it prints its name (av [0]) and the arguments specified in the call string. The OS sends the number of arguments (ac) and the arguments themselves (av) to the program as a vector of string addresses (an array of pointers to sequences of characters terminated by a binary zero). The first argument is the name of the command (the path to it. Call out from different tables of contents and look not print).
By the way, the parameter address vector ends with a zero address, i.e. av [ac] == NULL.