Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is It Necessary to Return a Value in Main()?

Tags:

c

return-type

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.

like image 776
nmagerko Avatar asked Dec 04 '11 15:12

nmagerko


2 Answers

The C99 standard says: (§5.1.2.2.1 Program startup)

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

So in a hosted environment, int is 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:

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

So you omitting a return from main is legal in C99, as long as your main returns an int.
(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.".)

like image 63
Mat Avatar answered Sep 28 '22 16:09

Mat


Summary: You don't necessarily have to, but you should.

In C90, reaching the end of main() without executing a return statement means "the termination status returned to the host environment is undefined". On at least one system I've used, the status returned is 1, which on that system indicate that the program failed.

C99 added a new rule, saying that reaching the end of main() returns 0. (C++ has the same rule.) Not all compilers fully implement C99, and those that do often don't behave as conforming C99 compilers by default. (Added 10 years later: The conformance situation is better now, and most modern C compilers support at least C99.)

The only portable values you can return from main() are 0, EXIT_SUCCESS, and EXIT_FAILURE (the latter two are defined in <stdlib.h>. 0 and EXIT_SUCCESS indicate that the program succeeded (and EXIT_SUCCESS is usually defined as 0); EXIT_FAILURE indicates that the program failed. return 1; is common, but non-portable; I've worked on a system (VMS), where a termination status of 1 indicates success. If you want your program to be portable, use EXIT_FAILURE to indicate failure; that's what it's for. Some systems and program define other system-specific or application-specific status codes.

For portability (and, IMHO, style), it's best to do an explicit return 0; at the end of main(), though it's not required in all circumstances. It's much easier to add that one line of code (which is, at worst, harmless) than to waste time determining whether you need it.

(Update 10 years later: I no longer think it's important to do an explicit return 0; unless you have a requirement for your code to work with old compilers or with current compilers configured to conform to the old C90 standard.)

Note that the correct definitions for main() are:

int main(void) { /* ... */ }

and

int main(int argc, char *argv[]) { /* ... */ }

or equivalent (for example, you can write char **argv rather than char *argv[]). It's questionable whether int main() { /* ... */ } is valid, for subtle reasons I won't go into here, but I believe all compilers accept it. Again, it's easier to add the void keyword than to waste time determining whether you need it.

A lot of books and tutorials use void main() or void main(void). A particular compiler may choose to permit this, but it's not portable. Seeing void main in a book or tutorial is a good sign that the author doesn't know the C standard very well, and that you should find something else to study.

like image 44
Keith Thompson Avatar answered Sep 28 '22 16:09

Keith Thompson