Hello!
Today in an interview my friend was asked this question.
Why C, C++ program execution starts with main()?
Is it a convention or is there some deeper underlying reason?
Thanks in advance!
Hello!
Today in an interview my friend was asked this question.
Why C, C++ program execution starts with main()?
Is it a convention or is there some deeper underlying reason?
Thanks in advance!
Every C program has a primary (main) function that must be named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of main, although it can terminate at other points in the program for a variety of reasons. At times, perhaps when a certain error is detected, you may want to force the termination of a program. To do so, use the exit function.
Functions within the source program perform one or more specific tasks. The main function can call these functions to perform their respective tasks. When main calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to main when a return statement is executed or when the end of the function is reached.
You can declare any function, including main, to have parameters. The term "parameter" or "formal parameter" refers to the identifier that receives a value passed to a function. When one function calls another, the called function receives values for its parameters from the calling function. These values are called "arguments." You can declare formal parameters to main so that it can receive arguments from the command line using this format:
When you want to pass information to the main function, the parameters are traditionally named argc and argv, although the C compiler does not require these names. The types for argc and argv are defined by the C language. Traditionally, if a third parameter is passed to main, that parameter is named envpCode:main( int argc, char *argv[ ], char *envp[ ] )
The compiler was designed to recognize main as the entry point in a program. It started that way with C, and C compilers were just built that way. It's the same way with C++ and Java just because of habit, consistency, standards,
etc.
According to the C standard, there is. It's just that the standard calls it "execution environment":
3.12
1 implementation particular set of software, running in a particular translation
environment under particular control options, that performs translation of programs for, and supports execution of functions in, a particular execution environment.
int main ()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
Bookmarks