Arguments from Operating System to Program in the C Computer Language
Part 21 of the Complete C Course
Foreword: Arguments from Operating System to Program in the C Computer Language
By: Chrysanthus Date Published: 4 Jun 2024
Introduction
int main() { /* ... */ }
or
int main(void) { /* ... */ }
or
int main(int argc, char *argv[]) { /* ... */ }
where \93{ /* ... */ }\94 represents the function body. The first two options are the same. The last option is what is being used in this chapter. The last option has two parameters: argc and *argv[]. Now, *argv[] is an array consisting of pointers. Each pointer points to a string. So the pointers are string pointers.
argc is an integer, a single number. These parameters for the main() function are not often used by a program in the main() function\92s body. They have not been used so far, in this chapter. They are only used, if data, in the form of a line of strings, are sent to the program, by the Operating System (terminal window). In that case, argc has the number of strings that are in the line. argv[0] of the second parameter has the name of the C executable file, put there due to the C compilation. The rest of the arguments, argv[1], argv[2], argv[3], etc. are input strings from the line typed, at the terminal, in order, separated by spaces; preceded by the name of the file. If an input string has a space, then that string is typed in double quotes (quotes of the text editor and not of the word processor) by the user (programmer). Read and test the following program that illustrates this, with the guide that follows:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%i\n", argc);
printf("\n");
printf("%s\n", argv[0]);
printf("%s\n", argv[1]);
printf("%s\n", argv[2]);
printf("%s\n", argv[3]);
return 0;
}
After compiling the program file, with the following command line (and pressing Enter):
gcc -o temp temp.c
where \93temp.c\94 is the source file and \93temp\94 is the executable file, run (execute) the program with (type and press Enter of keyboard):
./temp boy "big boy" man
The output should be:
4
./temp
boy
big boy
man
The value of argc is not typed in by the programmer. It is determined by part of the C program, included by the compiler. The input string values entered by the programmer (user), are put into the * argv[] array, also by part of the same C program, included by the compiler. These parts of the C program are not written by the programmer. The content of the main() function, has to be written into the main() function definition, by the programmer.
In the program above, the first printf() function call, in the main() function definition, prints the value held in the parameter, argc. This is the number of arguments sent in by the Operating System (terminal window); and the counting includes the name of the file. The second printf() statement, just prints an empty line at the output (to send the cursor below).
The value of argv[0], which is \93./temp\94 in this case, is the path and filename of the C executable file, which is \93temp\94 without the extension, \93.c\94. It is printed by the third printf() function call. argv[1] holds the second string argument, sent to the program (main() function) by the user (Operating System). In this case it is \93boy\94, and it is printed by the fourth printf() function call. argv[2] holds the third string argument, sent to the program (main() function) by the user (Operating System). In this case it is \93big boy\94, and it is printed by the fifth printf() function call. Since it consists of more than one word, it had to be typed in double quotes, at the terminal (Operating system). argv[3] holds the fourth string argument, sent to the program by the user (programmer). In this case it is \93man\94, and it is printed by the sixth printf() function call.
Thanks for reading. Meet you in the next tutorial.