Home>Article>Backend Development> Where does the execution of a c program start and end?

Where does the execution of a c program start and end?

青灯夜游
青灯夜游 Original
2020-08-31 16:45:09 23585browse

Where does the execution of a c program start and end?

The entrance of the C program is the main function. Generally speaking, as long as the normal operation ends, it starts from the first sentence of the main function and ends with the last sentence.

For example:

int main()//程序开始 { printf("Hello!\n"); return 0;//程序结束,返回值 }

But if the program encounters return (in the main function) exit (whether in the main function or a sub-function) in the middle of the execution of the program, the program will also end.

How to write the main function in C language

The main function is the entry function of the C program, that is, the execution of the program is Starting from the main function, calls to other functions are also directly or indirectly called in the main function. So who is calling the main function? The answer is the operating system. Since the development of C language, there are many different ways of writing the main function. Let’s explore the different ways of writing. Note: The test environment is Ubuntu 17.10, and the GCC version is 7.2.0.

NO.1

main(){}

Example:

#include ade979de5fc0e1ca0540f360a64c230b main(){ printf("Hello World\n"); }



NO.2##

void main(){}
Example:

#include ade979de5fc0e1ca0540f360a64c230b void main(){ printf("Hello World\n"); }


NO.3##

int main(){}
Example:

#include ade979de5fc0e1ca0540f360a64c230b int main(){ printf("Hello World\n"); return 0; }


NO.4

int main(void){}
Example:

#include ade979de5fc0e1ca0540f360a64c230b int main(void){ printf("Hello World\n"); return 0; }



NO.5

int main(int argc,char *argv[]){}
Example:

#include ade979de5fc0e1ca0540f360a64c230b int main(int argc,char *argv[]){ printf("Hello World\n"); return 0; }

NO.6

int main(int argc,char **argv){}
Example:

#include ade979de5fc0e1ca0540f360a64c230b int main(int argc,char **argv){ printf("Hello World\n"); return 0; }

From the above test, writing method 1 It can be compiled normally under the C90 standard, but a warning will be thrown under the C99 and C11 standards (the return value is not written, the default return value is int, which is equivalent to writing method 1 and writing method 3). Writing methods 2-6 can be compiled normally under the C90, C99, and C11 standards. Among so many ways of writing, which one is the standard way of writing the main function? So I checked the C standard document for the standard way of writing the main function, see the picture below:

## It can be seen from the standard document that the writing methods 4, 5, and 6 are This is the standard way of writing the main function. Writing method 5 is equivalent to writing method 6. Then why are there writing methods 1, 2, and 3? That's because from the time when the C language was designed in 1972 to the release of the C90 standard, different implementations of the C language resulted in differences in the main function. Writing method 2 is strongly not recommended because the main function is called by the operating system. The operating system will judge whether the program is executed correctly based on the return value of the main function. If void is returned, what state does it represent? Moreover, some compilers support this writing method, some compilers do not support it, and all standards do not recognize this writing method. Writing methods 1 and 3 are still barely acceptable, but it is not recommended. It is best to write according to the standard writing method. Why should we write according to the standard writing method? That's because in order to make C programs more portable. Writing method 5 is equivalent to writing method 6. The first parameter represents the number of main function parameters, and the second parameter uses pointers to point to these parameters respectively. argv[0] represents the program name, argv[1] to argv[argc-1] represent program parameters. Let’s take a look at the return value of the main function, see the picture below

It can be seen from the figure that if the return value of the main function is a compatible type of int type, the value returned from the main function together with exit is equivalent to executing exit xxx. xxx represents the value returned from the main function. If you forget to write the return statement, when the closing brace } of the main function body is executed, the default value 0 will be returned. Returning 0 means that the program execution is successful and the program exits. If the return type of the main function is not a compatible type of int type, the status returned by the program to the operating system will be unclear.

C standard document download address:

Portal: http://download.csdn.net/download/u012219371/10184521

C standard introduction:

Portal: http://blog.csdn.net/u012219371/article/details/78951972

Related recommendations:c language tutorial video

The above is the detailed content of Where does the execution of a c program start and end?. 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