main() at bottom of program

This is a discussion on main() at bottom of program within the C Programming forums, part of the General Programming Boards category; Is there any benefit, or even difference in having the main() function at the bottom of the code, instead of ...

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

    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?

    Thanks

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    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
    Posts
    66
    Handy.

    THanks.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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 function..so 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
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

    Quzah.
    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, 02: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, 12:38 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21