I get this code:
#include<stdio.h>
#include<stdlib.h>
void main(void)
{
char *ptr = (char*)malloc(10);
if(NULL == ptr)
{
printf("\n Malloc failed \n");
return;
}
else
{
// Do some processing
free(ptr);
}
return;
}
It compiles successfully in Visual C, but do not compile in gcc, I get "error:'main' must return 'int'". So is the return type 'int' of main() function is a convention(which is for compiler to define), or a rule of C?
The C standard (ISO/IEC 9899:2011) says:
5.1.2.2.1 Program startup
1 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 ofint
and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, 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;10) or in some other implementation-defined manner.
10) Thus,
int
can be replaced by a typedef name defined asint
, or the type of argv can be written aschar **argv
, and so on.
Thus, the only portable declaration for main()
has a return type of int
. If MSVC defines that void
is permitted ('or in some other implementation-defined manner'), so be it, but do not expect the code to be portable. The old versions of the Microsoft compilers (up to and including MSVC 2005) do not permit void main()
: see the documentation at main
: Program startup and The main
Function and Program Execution. However, MSVC 2008 and later are documented to allow void main()
: see main
: Program Startup. The three-argument form of main()
is noted as a common extension in Appendix J:
J.5 Common extensions
The following extensions are widely used in many systems, but are not portable to all implementations. The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers, or predefined macros with names that do not begin with an underscore.
J.5.1 Environment arguments
In a hosted environment, the
main
function receives a third argument,char *envp[]
, that points to a null-terminated array of pointers tochar
, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1).
The value returned from main()
is transmitted to the 'environment' in an implementation-defined way.
5.1.2.2.3 Program termination
1 If the return type of the
main
function is a type compatible withint
, a return from the initial call to themain
function is equivalent to calling theexit
function with the value returned by themain
function as its argument;11) reaching the}
that terminates themain
function returns a value of 0. If the return type is not compatible withint
, the termination status returned to the host environment is unspecified.11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in
main
will have ended in the former case, even where they would not have in the latter.
Note that 0
is mandated as 'success'. You can use EXIT_FAILURE
and EXIT_SUCCESS
from <stdlib.h>
if you prefer, but 0 is well established, and so is 1. See also Exit codes greater than 255 — possible?.
7.22.4.4 The
exit
function¶5 Finally, control is returned to the host environment. If the value of
status
is zero orEXIT_SUCCESS
, an implementation-defined form of the status successful termination is returned. If the value ofstatus
isEXIT_FAILURE
, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.