Home  >  Article  >  Backend Development  >  Where can the main function be located in C language?

Where can the main function be located in C language?

王林
王林Original
2020-07-18 10:58:1912310browse

The main function in C language can be at any position. Each C language program has one and only one main function (main()), and the program must start with the main() function, which is the first function called when the program starts. Function definition form: [int main(void){}].

Where can the main function be located in C language?

C language stipulates that in a C program, the main() function can be at any location.

(Recommended tutorial: c language tutorial)

Analysis:

Each C program has one and only one main function (main), and the program Execution must start from the main() function, and the main() function can be placed anywhere in the program.

Detailed introduction:

A C program compiled in the host environment must define a function named main, which is the first function called when the program starts.

The definition of main() function has the following two forms:

(1) The function has no parameters and the return value is int type.

int main( void ) { /* … */ }

(2) The function has two parameters, the types are int and char**, and the return value is of type int.

int main( int argc, char *argv[ ] ) { /* … */ }

Both definition methods are consistent with the C language standard. In addition, many C implementations also support the third, non-standard syntax definition:

int main( int argc, char *argv[ ], char *envp[ ] ) { /* … */ }

The function return value is int and has 3 parameters: the first is int, the other two It's char**.

The above is the detailed content of Where can the main function be located in C language?. 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