Broad Network


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

Any C program must have the main() function. In an application, consisting of more than one file, only one file should have the main() function. This is the principal file. At program startup, the operating system (terminal window or command prompt window) sends control to the main() function. In other words, the first entity to execute when a C program starts, is the main() function. The main() function uses the following syntax:

        int main() { /* ... */ }
or
        int main(void) { /* ... */ }
or

        int main(int argc, char *argv[]) { /* ... */ }

where “{ /* ... */ }” 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’s 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 “temp.c” is the source file and “temp” 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 “./temp” in this case, is the path and filename of the C executable file, which is “temp” without the extension, “.c”. 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 “boy”, 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 “big boy”, 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 “man”, and it is printed by the sixth printf() function call.

Thanks for reading. Meet you in the next tutorial.

Related Links

More Related Links

Cousins

BACK NEXT

Comments