Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

What should main() return in C?

The accepted answer appears to be targetted for C++, so I thought I'd add an
answer that pertains to C, and this differs in a few ways. There were also 
some changes made between ISO/IEC 9899:1989 (C90) and ISO/IEC 9899:1999 (C99).

main() should be declared as either:

int main(void)
int main(int argc, char **argv)

Or equivalent. For example, int main(int argc, char *argv[]) is equivalentto 
the second one. In C90, the int return type can be omitted as it is a 
default, but in C99 and newer, the int return type may not be omitted.

If an implementation permits it, main() can be declared in other ways 
(e.g., int main(int argc, char *argv[], char *envp[])), but this makes the 
program implementation defined, and no longer strictly conforming.

The standard defines 3 values for returning that are strictly conforming 
(that is, does not rely on implementation defined behaviour): 0 and
EXIT_SUCCESS for a successful termination, and EXIT_FAILURE for an
unsuccessful termination. Any other values are non-standard and 
implementation defined. In C90, main() must have an explicit return 
statement at the end to avoid undefined behaviour. In C99 and newer, you
may omit the return statement from main(). If you do, and main() finished, 
there is an implicit return 0.

Finally, there is nothing wrong from a standards point of view with calling main() recursively from a C program.
 
PREVIOUS NEXT
Tagged: #What #return
ADD COMMENT
Topic
Name
2+1 =