Home>Article>Backend Development> What is the only function in C language?

What is the only function in C language?

烟雨青岚
烟雨青岚 Original
2020-07-06 17:16:21 4000browse

The only function in C language is: "main" function. The "main" function, also known as the main function, is the starting point of program execution; if there are other functions, it will complete the calls to other functions and then return to the main function. Finally, the "main" function ends the entire program.

What is the only function in C language?

The only function in C language is: main function

main function, also known as main function Function is the starting point of program execution

Program execution always starts from the main function. If there are other functions, it will return to the main function after completing the calls to other functions, and finally ends with the main function. the entire program. When executing the program, the main function is called by the system.

The main function is called after the initialization of non-local objects with static storage duration is completed during program startup. It is the designated entry point for a program in a hosted environment (that is, an operating system). Entry points for stand-alone programs (boot loaders, operating system kernels, etc.) are implementation-defined.

The formal parameters in the two formal parameter forms of the main function allow arbitrary multi-byte strings to be passed from the execution environment (they are often called command line parameters), each pointer argv[1]. .argv[argc-1] points to the first character of each of these strings. argv[0] is a pointer to the first characters of a null-terminated multibyte string (or the empty string "" when the execution environment does not support it) representing the name of the program itself used to execute it.

These strings can be changed, although changes to them will not be passed back to the execution environment: for example, they can be used with std::strtok. The size of the array pointed to by argv is at least argc 1, and its last element argv[argc] is guaranteed to be a null pointer.

In the latest C99 standard, only the following two definitions are correct:

int main( void ) /* 无参数形式 */ { ... return 0; }
int main( int argc, char *argv[] ) /* 带参数形式 */ { ... return 0; }

int specifies the return type of the main() function, and the parentheses after the function name Typically contains information passed to the function. void means no parameters are passed to the function. We will discuss the parameterized form later.

Browsing the old version of C code, you will find that the program often starts with.

main()

This form begins. The C90 standard allows this form, but the C99 standard does not. So even if your current compiler allows it, don't write it like this.

There is another form you may have seen.

void main()

Some compilers allow this form, but no standard has considered accepting it. Bjarne Stroustrup, the father of C, clearly stated in the FAQ on his homepage: The definition of void main() never existed in C or C. Therefore, the compiler does not have to accept this form, and many compilers do not allow it to be written this way.

The point of insisting on using standards is that when you move your program from one compiler to another, it will still run normally.

Recommended tutorial: "C Language"

The above is the detailed content of What is the only function 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