The execution of the C program starts from the main function. If other functions are called in the main function, the process is returned to the main function after the call, and the entire program is ended in the main function.
Some inexplicable texts in this section have become a problem even from the perspective of Chinese Syntax. The problem is that "the process returns to the main function after the call" does not know whether it is a result of the previous "if" or a supplement to the previous "if. Aside from the disadvantages of this language, this article gives the impression that "C Programs start to execute from the main function and end with the main function ". But is that true?
A certain environment (Environment) is indispensable for running C Programs. Such an environment is called execution environment ). There are two types of environments: freestanding environment and hosted environment ).
The so-called independent environment means that the program is not run by the operating system, while the host environment means that the program is executed under the control of the operating system.
In these two environments, the program startup flag isA specified functionStart to be called.
In an independent environment, the name of the called function is not necessarily main, but determined by the compiler. This is called implementation-defined ). Even the type of the function that is first called is also defined by the implementation.
Only in the host environment, the running of the C program starts from main.
Therefore, the phrase "C program execution starts from the main function" is only true under certain conditions and is one-sided.
As for the function at which the program ends, the C language has never been specified. The program may end in main () or other functions. Many functions in the C standard library are related to the ending program. The function prototype of these functions is described in stdlib. H, for example
Void abort (void );
Void exit (INT );
The following code is a simple example that does not end the program in main:
#include “stdlib.h”
void fun ( void ) ;
int main ( void ){
fun();
return 0;
}
void fun ( void ){
exit(1);
}
It ends in the fun () function.