I was just writing a quick program for calculating some things when I came across the return statement/exit statement for the C program.
I declared main() to be of type int, so I would have to put in a return of an integer, or my program would not compile correctly. However, is it acceptable to make main a Boolean or even void?
I know the standard way to create a C program is to return a value so any problems can be sorted out, among other things, but wouldn’t a Boolean work the same way? Also, could I get away with declaring it void and not having problems with the operating system still running my program after it has been terminated?
Thanks for any and all help.
The C99 standard says: (§5.1.2.2.1 Program startup)
So in a hosted environment,
intis the only valid, standard return type. Implementations can define other entry points though.Note that section §5.1.2.2.3 Program Termination has this:
So you omitting a return from
mainis legal in C99, as long as yourmainreturns anint.(But previous versions of the C standard didn’t have that exception for
main– returning no value (or reaching the final}without a return statement) causes “the termination status returned to the host environment [to be] undefined.”.)