Thread: main() at bottom of program

  1. #1
    Registered User
    Join Date
    Mar 2004

    main() at bottom of program

    Is there any benefit, or even difference in having the main() function at the bottom of the code, instead of the top.

    I also notice if main() is at the bottom, the functions dont need to be declared. Why is this?


  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Newport, South Wales, UK
    Nope, there's no difference where main() is in your program, just so long as it's there.

    The reason why you don't need prototypes if it's at the bottom is because the compiler reads the source file from the top to the bottom, defining symbols as it goes along. Therefore, by the time it gets to main(), it already knows about all the functions you're gonna call.

  3. #3
    Registered User
    Join Date
    Mar 2004


  4. #4
    Registered User
    Join Date
    Nov 2004
    I also notice if main() is at the bottom, the functions dont need to be declared.
    well, in C you dont have to declare a function. in C++ you have to..if you try to compile the program in C when the function is defined after main() without giving prototypes, you might get warnings, but the program will work, but there might be issues with the return type of the function. the dev compiler for example will take int to be the default return type if there is no declaration of the function prototype before calling the its always a good practise to give the prototype first.

    as far as main being at the bottom of the program, it depends on how you like it, whether you want to put main() at the start or at the end..i usually prefer main() to be at the bottom with least amount of code inside main() itself..let all the other functions do the job.

    and yes, the compiler goes from top to bottom as SMurf said. So , it wont matter where the main() is, so long as it is there.. if its there, the compiler will find it for you
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    No. This is wrong. Just because your compiler does the linking for you, doesn't mean this is allowed behavior. All functions need to be 'visible' before you use them. This means either the function is declared in scope before you use it, or it has an available prototype in scope before you do so. Doing anything else is wrong, whether your compiler skirts around it or not.

    As to main, it doesn't matter where it's declared. It can be in its own file if you like, so long as the above rules are followed.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM