C language architecture-Inevitability of the Main Function)

Source: Internet
Author: User

Note: It is not original. It is what we hear in the classroom and prevents forgetting. So remember it!

The architecture of C language is divided into two parts: Data and functions. Of course, these two parts will eventually enter the computer's memory.

After the function is compiled, the code can be programmed and stored in the code area. The data can be divided into local and global, auto and static. Simply put, the data can be stored in the static data area, heap, and stack.

Global variables and static variables are created before the main function.

The C language requires that no executable code exists in the global zone and the executable code must enter the function. However, the functions in C language are global, so that functions cannot be nested: nested definitions make the functions defined inside the function a local function. Therefore, to solve the issue of executing each function, you can only use the nested call of the function. In this case, a function must be first executed to call a series of other functions to complete the functions of the program. The first function called is the main function.

The above is the inevitability of the existence of the main function. I am not good at writing, but I can't explain it clearly.

However, the above system encountered conflicts in C ++. The global objects of C ++ are pushed from the architecture of C prior to the execution of the main function. The global object must be created before the main function is executed, and the constructor must be executed before the main function is executed. When debugging with the following code, the result shows that the constructor is executed earlier than the main function.

# Include <iostream>

Class t

{

Public: T () {cout <"constructor" <Endl ;}

}

T;

Int main (void)

{

Cout <"Main" <Endl;

Return 0;

}

This conflicts with the C language architecture. Therefore, C ++ is not a pure object-oriented programming language. As for how to solve this system conflict, it is determined by the compiler. It may also be the first to enter the main function, but then jump to the constructor to create class objects and then return to the Code for executing the main 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.