C main function and command line parameters (30)

        We know that in the C language, the program starts to run from the main function, which we call the main function . Let's see if the following main functions are defined correctly?


picture.png

        So what exactly is the prototype of the main function? Let's see what the compiler says, let's compile the next four mains respectively

 In the form of a function, after compiling, the program can be compiled and executed. Then the prototype of the most standard main function is the fourth type above. The main function is a function called by the operating system. The operating system always uses the main function as the beginning of the application and the return value of the main function as the exit status of the application . So why do C compilers support so many different main function prototypes? Let's do an experiment, the code is as follows

#include <stdio.h>

intmain()
{
    printf("hello\n");
    
    return 99;
}

        Let's compile it under the BCC compiler, and the result must be to print hello. But what we returned is 99, let's print the value of the environment variable by the way to see what's the mystery

picture.png

        We see that the value of the environment variable printed is 99. If we change the return in the above program to 0. Then compile and see what the value of the environment variable is.

picture.png

        We see that the value of the environment variable has become 0. Then the return value of the main function is to return it to the system and save it. So back to the question we said before, in the previous program, there are many programming methods. In order to be compatible with all previous programs, the compiler must support the writing of all main functions. Then the program can pass parameters to the main function in the format: int main(int argc, char *argv[], char *env[]) . a> argc - number of command line parameters; b> argv - command line parameter array; c> env - environment variable array ; most of the main functions we usually see are written with the first two.

        Then in the gcc compiler, the common usage is as follows:

picture.png

        Let's take a look at a sample code, the code is as follows

#intclude <stdio.h>

int main(int argc, char* argv[], char* env[])
{
    int i = 0;
    
    printf("============== Begin argv ==============\n");
    
    for(i=0; i<argc; i++)
    {
        printf("%s\n", argv[i]);
    }
    
    printf("============== End argv ==============\n");
    
    return 0;
}

        Let's take a look at the compilation effect

picture.png

        We can see that ./a.out is printed. That is to say, the command line parameters other than gcc are printed out. Let's print it in the following way.

picture.png

        Then we can see that all parameters except gcc are printed. At the end, let's discuss an interesting question: Does the main function have to be the first function executed by the program? As soon as I heard it, that's right, what we usually see in books and what the teacher said is like this. So let's do an experiment, the code is as follows

#include <stdio.h>

#ifndef __GNUC__
#define __attribute__(x) 
#endif

__attribute__((constructor))
void before_main()
{
    printf("%s\n",__FUNCTION__);
}

__attribute__((destructor)) 
void after_main()
{
    printf("%s\n",__FUNCTION__);
}

intmain()
{
    printf("%s\n",__FUNCTION__);
    
    return 0;
}

        We see that if it is a GUNC compiler, the __attribute__ macro is defined. Through this macro, we declare two functions separately, let's take a look at the compilation results

picture.png

        We see that these two functions are executed before and after the main function. It feels amazing, then this __attribute__ macro is an attribute keyword unique to our gcc compiler. Use it to execute other functions before and after the execution of the main function. Let's take a look at the BCC compiler, is it still supported?

picture.png

        We see that it doesn't support that __FUNCTION__ , so we replace it with the corresponding string. The result after compilation is that only main is printed, which means that it does not execute the two functions before and after the main function. So this feature is also supported by the compiler, but at least it shows that calling other functions before the main function is supported in modern compilers .


        Welcome to learn C language together , you can add me QQ: 243343083 .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324739744&siteId=291194637