Results 1 to 5 of 5

Thread: Why C, C++ program execution starts with main()

  1. #1
    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!

  2. #2
    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

  3. #3
    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.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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.

  5. #5
    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.

Similar Threads

  1. Replies: 4
    Last Post: 29-12-2010, 06:49 AM
  2. Execution of program can not find the file
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 13-01-2010, 11:42 AM
  3. Procedure to run Java program without main method
    By Sheena_thakkar in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 02:57 PM
  4. Delay in execution of C program?
    By Chrisch in forum Software Development
    Replies: 4
    Last Post: 03-10-2009, 10:52 AM
  5. Iphone Error "Main Script Execution Failed"
    By kamina23 in forum Portable Devices
    Replies: 2
    Last Post: 26-08-2009, 11:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,047,856.43017 seconds with 16 queries