Sunday 13 November 2011

Should programs always include a prototype for main()? in C programming

Should programs always include a prototype for main()?

Sure, why not? Including the prototype is not required, but it is good programming practice. Everyone knows what the argument types are for main(), but your program can define the return type. You’ve probably noticed in the examples in this chapter (and possibly in other chapters) that main() is not prototyped, that no explicit return type is shown in the main() body itself, and that main() does not even contain a return() statement. By writing the examples in this way, I have implied a void function that returns an int. However, because there is no return() statement, a garbage value will be returned. This is not good programming practice. Good programming practice dictates a function prototype even for main(), and a proper return value for that prototype.

Cross Reference:

XIV.12: Should main() always return a value?

No comments:

Post a Comment