Thread: Good reasons for placing main at the end of the program

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    Good reasons for placing main at the end of the program

    Hi,

    In many programming references, I have found that in programs with several functions in the same .c file, that main() is often placed at the end of the file. While this seems to be counter intuitive as main is executed first, I have just come across a case where this seemed to matter.

    So is this a matter of style, or is there a good technical reason for laying out programs in this way?

    --dave

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The idea behind this comes from Pascal, here essentially you HAVE to put main at the end.

    In C, you can put functions any any order you like, main is no different.

    I like to put main at the bottom - partly because I started out programming in Pascal, and partly because it means that I don't have to make forward declarations of functions. You don't need prototypes of functions in the same source file [unless you have mutual recursion] if you put the functons that call other functions below the ones being called.


    Beyond that, there is no particular reason for any particular order - it's a matter of style [and I know some people who prefer to find main and functions called by main first, and then the detailed functons below that].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    thanks mats you are informative as always

    --dave

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Oh, some compilers tend to only inline functions that are above the place where they are being inlined too - basically, it doesn't inline functions if it encounters the call first, then the function. Most modern compilers do not have this limitation, but I guess it may still be true for some smaller/older embedded compilers for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C Video Tutorial Collection]
    By Chernobyl in forum C Programming
    Replies: 17
    Last Post: 04-07-2007, 01:24 AM
  2. Need help with my program, please take a look
    By Hermisky in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2006, 10:13 AM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. Program was working good until.....
    By cazil in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2002, 10:53 PM

Tags for this Thread