Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-02-2009
Member
 
Join Date: Jan 2009
Posts: 18
Why C, C++ program execution starts with main()

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!
Reply With Quote
  #2  
Old 13-02-2009
Member
 
Join Date: Jan 2009
Posts: 44
Re: Why C, C++ program execution starts with main()

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:

Code:
main( int argc, char *argv[ ], char *envp[ ] )
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 envp
Reply With Quote
  #3  
Old 13-02-2009
Member
 
Join Date: Jan 2009
Posts: 38
Re: Why C, C++ program execution starts with main()

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.
Reply With Quote
  #4  
Old 13-02-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Why C, C++ program execution starts with main()

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.
Reply With Quote
  #5  
Old 13-02-2009
Member
 
Join Date: May 2008
Posts: 35
Re: Why C, C++ program execution starts with main()

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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Why C, C++ program execution starts with main()"
Thread Thread Starter Forum Replies Last Post
Make a program behave on the basis of input given dring execution Karumbu Software Development 4 29-12-2010 06:49 AM
Execution of program can not find the file Gunner 1 Software Development 5 13-01-2010 11:42 AM
Procedure to run Java program without main method Sheena_thakkar Software Development 3 26-11-2009 02:57 PM
Delay in execution of C program? Chrisch Software Development 4 03-10-2009 10:52 AM
Iphone Error "Main Script Execution Failed" kamina23 Portable Devices 2 26-08-2009 11:44 PM


All times are GMT +5.5. The time now is 09:56 PM.