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?
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?
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.
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
There are two rules of thumb:
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
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.