"Does the C program start to run from the main function and end with the main function?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.