[Courses] Absolute Beginning C: void?

Kelly Martin kelly.lynn.martin at gmail.com
Mon Feb 6 06:07:33 UTC 2012


On Sun, Feb 5, 2012 at 11:47 PM, Mel Chua <mel at melchua.com> wrote:
>
> I just tried these three things out:
>
> 0) Running the original program with the -Wall option to make sure I got
> the same error message that Akkana described. (I did.)
>
> 1) changing the 2nd line to 'int main()' and adding 'return 0;' at the
> end of the main() function. (This worked perfectly, no error
> messages/warnings whatsoever, awesome.)
>
> 2) changing the 2nd line to 'void main()' (with no 'return' statement at
> the end of the main () function). (This resulted in a warning:
> 'welcome.c:2:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]')
>
> So it looks like gcc doesn't like it when main() is told to return another
> type of value -- is there any particular reason why, and is this something
> unique to the main() function?
>
> --Mel, who's was taught C in the school of "kludge 'till it works well
> enough for competition/demo because we won't have to maintain/extend it
> afterwards" by a bunch of other high school and college kids, which
> unfortunately explains a lot of my coding style/abilities.
>
> ______________________________**_________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/**mailman/listinfo/courses<http://mailman.linuxchix.org/mailman/listinfo/courses>
>

The "main" function is expected to return an int.  The returned value (or
at least the low eight bits of it) is returned to the calling process as
the exit code of the program.  Customarily a return of zero indicates
success and a return of one indicates failure, but some programs make
different use of the return code.  If main returns something other than an
int, or nothing at all, then the program's exit code becomes "undefined"
(which means it'll be random, or at least difficult to predict).

This behavior is specific to the main function.

Kelly


More information about the Courses mailing list