Home  >  Article  >  Backend Development  >  The main function is the only starting point of C language, right?

The main function is the only starting point of C language, right?

王林
王林Original
2020-07-06 09:27:023781browse

The main function is the only starting point of C language, yes. When an independent C language program is executed, there must be a main function. The main function is the entry function, which means that the program will first execute the main function. Therefore, the main function command cannot be written in other forms.

The main function is the only starting point of C language, right?

The main function is the only starting point of C language, yes.

(Recommended learning: C language tutorial)

Detailed introduction:

When an independent C program or C program is executed, it must There is a main function. The main function is the entry function, which means that the program will execute the main function first.

Therefore, the main function name cannot be written in other forms. Non-independent programs do not need main. For example, algorithms developed for other programs or third-party companies do not have a main function because it is not an independent program and needs to be called by other modules.

When the program is executed, we do not manually call the main function, so where is it called and how is it started?

In fact, when the program is compiled, the compiler will add a startup code to the program, and the main function is called by this startup code. It is the bridge between the main function and the operating system.

Simply put, the main function is called by the operating system through the startup code.

Let’s take a look at how to write the main function:

//C语言形式
main()                    //省略返回类型,说明返回值为int类型,C++逐步淘汰了这种用法
{
    return 0;
}

//变体形式
int main(void)     //在C++中,与标准形式等效;在C语言中,让括号空着表示对是否接收参数保持沉默
{
    return 0;
}

//变体形式,在某些平台可能无法正常工作
void main()
{
}

The above is the detailed content of The main function is the only starting point of C language, right?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn