Who calls main() in C++ programs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abhinay
    New Member
    • Jul 2006
    • 44

    Who calls main() in C++ programs

    Hi,

    This is abhinay,
    I always suffered with following questions,
    I red many docs but I could't gets its proper answer,
    if u can help me then plz......
    thanks in advance.

    My questions are:

    1. Which code or process is responsible for first call to main() in C/C++ programs.?

    2. main() is user defined function still why main() is geting called first. ?

    3. Which doc should i read for detailed information.

    Thanks
    Abhinay
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Abhinay
    Hi,

    This is abhinay,
    I always suffered with following questions,
    I red many docs but I could't gets its proper answer,
    if u can help me then plz......
    thanks in advance.

    My questions are:

    1. Which code or process is responsible for first call to main() in C/C++ programs.?

    2. main() is user defined function still why main() is geting called first. ?

    3. Which doc should i read for detailed information.

    Thanks
    Abhinay
    1: The code that starts running first is the 'startup code'. Traditionally it's called
    'c0' (unixes) or 'crt0' (MS windows). The code does some initialization work
    and then basically performs this action:
    Code:
    exit(main(...));
    i.e. it takes the return value of main and returns the completion status back to
    the os again (which called c0 or crt0).

    2: The startup code expects your code to supply a main() method.

    3: any good book or manual for C/C++ compilers should give you all the info.
    If you're working on a unix platform check the man pages.

    kind regards,

    Jos

    Comment

    • svlsr2000
      Recognized Expert New Member
      • Feb 2007
      • 181

      #3
      You can have look at how linkers and loaders work.

      Its not always necessary to have a main, the compiler i use has a option of setting the entry function.

      Comment

      Working...