Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it good practice to have a main.c file that just has the main function in it and no other functions so that all other functions can be interfaced?

If there is no definitive rule, when is it good to do so and not to do so?

share|improve this question
up vote 12 down vote accepted

Ideally, all work which can be thought of as part of re-usable code, should be created in the form of library. The balance work, is application which should be separate where main() will reside.

But main() alone doesn't have to sit in isolation. Functions like parse_arguments(argc,argv) should be along with main rather then separate.

share|improve this answer

Our coding standard requires that main() is in main.c. Other methods in main.c tend to be high level error handlers and helper functions for main (refer @Dipan answer "parse_arguments", as well as things like "display_help" etc.

A good rule to go by is when a funcion starts to do more than support the running application and start doing business logic, it's time it was out of main.c

share|improve this answer

There are two rules of thumb:

  1. The reader shouldn't need to wonder where you have hidden main().
  2. main() should not contain irrelevant clutter.

To implement a coding standard stating that main() should always be located in a file called main.c is both good and common practice. This file, as well as main() itself, should not contain unnecessary clutter.

Ideally main() and main.c should only contain the following

  • Includes of program-wide header files.
  • Argument parsing code of argv, argc.
  • On hostless systems: critical register setups, stack pointer setup etc. But only if main() is the first point of entry for the program.
  • Calls to start up the OS, or a loop calling the program's state machine, or in hosted desktop systems, creation and initialization of windows related to the main GUI thread.
  • Internal (static) functions that are called from main(), handling any of the above.
  • On a hosted system: return 0.
share|improve this answer

A clean entry point in a separated file makes the code flow to be easily understood and maintained. I have always have had this habit of keeping a very small and concise main() function in a different file from where onwards I can trace the program steps. Just for the sake of cleanliness it is good to keep it separate.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.