Thread: int main() question

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    int main() question

    i know int main is the basic function of the program but what is its purpose and how does it affect a program when int main is placed at the end of the prog or when its placed at the beginning? does this matter for the program or is it a programmers style?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It only has to exist
    Where you put it in your code doesn't matter

    > but what is its purpose
    To basically say "start here"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    but if int main is the function that says the program where to start that means int main runs b4 any other function correct? so why then some programs have the int main at the end. the code b4 it what does it do? maybe my question wasnt clear, i meant if int main starts the prog and all other functions follow y r they written sometime in the beginning? they have to wait for int main to start?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> so why then some programs have the int main at the end.
    The actual order of the source file is irrelevant, the compiler takes care of putting things into their correct places in the executable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > means int main runs b4 any other function correct?
    Correct

    > so why then some programs have the int main at the end. the code b4 it what does it do?
    Style
    The position of a function has nothing to do with when it gets executed
    Code:
    void foo ( ) { }
    void bar ( ) { }
    int main ( ) {
      bar();
      foo();
      return 0;
    }
    Reorder it however you want, doesn't make a difference.

    Put foo() in foo.c and bar() in bar.c (this is typical of large programs spanning hundreds, possibly thousands of source files). Only one of them has main(), and it appears just once.

    > they have to wait for int main to start?
    You're thinking in terms of some ancient interpreted language like BASIC
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Quote Originally Posted by Salem

    > they have to wait for int main to start?
    You're thinking in terms of some ancient interpreted language like BASIC
    cool , so the other functions have been run and int main only tells where to start and then the order of execution begins, correct?

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Functions don't "run" until called. Main will be the starting point for the entire program and as it encounters function calls it goes to that function which runs until it encounters another function call or it ends.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Main will be the starting point for the entire program
    An exception to the rule?:
    Code:
     #include <iostream>
     
     class myClass
     {
       public:
         myClass()
         {
           std::cout <<"Class being created!" <<std::endl;
         }
     };
     
     myClass c1;
     
     int main(void)
     {
       std::cout <<"main being run!" <<std::endl;
     }
     
     /*
     
     Output:
     
     Class being created!
     main being run!
     
     */
    Here the object is created and it's constructor is invoked as part of the process. As its global, it gets created before main gets to run.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    So in summary, objects with file scope are created first, and their constructors are called, and after all the constructors have returned, then int main() is the next thing to run.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. I need help with variables, please
    By JOCAAN in forum C++ Programming
    Replies: 39
    Last Post: 12-08-2007, 04:16 PM
  3. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 11:06 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM