[Courses] C Programming For Absolute Beginners, Lesson 5A: All About Functions, Part 2

Sachin Divekar ssd532 at gmail.com
Mon Apr 2 19:07:34 UTC 2012


>
> What is the difference between:
>
> void functionname (void)
>
> and
>
> int functionname (void)
>
> Why is it OK to use 'void functionname (void)' for ordinary functions,
but not
> OK to use 'void main (void)' ?
>


Defining return type of main() as int allows a return code to be passed to
the invoker
i.e. the user or the calling process. Users can refer this exit status for
debugging.
Or invoking programs can use this return code for something useful

And as it is required by standard, not defining return type as int can
create lots
of problems.

Refer following page on avoiding void main(void).

http://users.aber.ac.uk/auj/voidmain.cgi

Excerpt from the page:

-X-

Because you are likely to return a random value to the invokation
environment. This is bad, because if someone wants to check whether your
program failed, or to call your program from a makefile, then they won't be
able to guarantee that a non-zero return code implies failure.
(To which the answer is usually of the form "that's their problem").

This page demonstrates a system on which a void main(void) program will
very likely cause problems in the third class above. Calling the program
from a script may cause the script to die, whether or not its return code
is checked. Calling it from a makefile may cause make to complain. Calling
it from the command line may cause an error to be reported.


-X-


Ordinary functions are not special functions like main(). So using void
functionname (void) will
not have much problems. And returning int for every such trivial function
will also be a headache.

--
Regards,
Sachin Divekar


More information about the Courses mailing list