This is my program:
main()
{
printf("hello world\n");
}
I get this warning when compiling it:
function should return a value
When changing main()
to void main()
, the warning disappears.
Why is that so?
This is my program:
main()
{
printf("hello world\n");
}
I get this warning when compiling it:
function should return a value
When changing main()
to void main()
, the warning disappears.
Why is that so?
There are few things which you should take note of :
int
is the main()
function's return type. That means that the kind of value main()
can
return is an integer.main( )
was tolerated by the C90 compilers but not by C99 compilers which means its not a part of C99 standard anymore , so don't do this.void main()
is not a standard form ,some compilers allow this, but none of the standards have ever listed it as an option. Therefore,
compilers don't have to accept this form, and several don't. Again, stick to the standard form,
and you won't run into problems if you move a program from one compiler to another.And one last thing , instead of writing main like this :
int main()
// here you are being silent about passing arguments to main , meaning it may or may not take arguments
write like this :
int main(void)// this specifies there are no arguments taken by main
You might wanna look at the C99 standard for further details.
Quick summary: If you don't want to use command-line arguments, you should write:
int main(void) {
/* body of main function */
}
If you do:
int main(int argc, char *argv[]) {
/* ... */
}
These are the only portable ways to define the main
function.
You should probably have a return 0;
at the end, though it's not strictly necessary. Returning 0
indicates successful execution. There are ways to indicate that execution failed; I won't get into that here.
There's some history behind this. The rules for a valid definition of the main
function have changed a bit across different releases of the C standard.
Before the introduction of the first official standard for C in 1989, the most common form was:
main()
{
/* ... */
}
Or, if you wanted to use command-line arguments:
main(argc, argv)
/* argc is implicitly of type int */
char *argv[];
{
/* ... */
}
There was no way to define a function that didn't return a value. If you didn't specify a return type, it defaulted to int
.
The 1989 ANSI C standard (which was republished with editorial changes as the 1990 ISO C standard) introduced prototypes, function declarations and definitions that specify the parameter types. There are two equally valid definitions for main
. You can use one or the other depending on whether you need to use command line arguments:
int main(void) {
/* ... */
}
or
int main(int argc, char *argv[]) {
/* ... */
}
(char *argv[]
can also be written as char **argv
. This rule applies only to parameter definitions.)
A given compiler may or may not choose to permit other forms. For example, some compilers support a third parameter envp
.
Somehow, some authors have gotten the idea that void main()
or void main(void)
is valid. It can be valid for some particular compiler, but only if that compiler explicitly supports it. It's not portable. The odd thing about this is that the same standard that first introduced the void
keyword simultaneously established the rule that main
's return type is int
.
void main()
is useful as an indicator that the author of the book you're reading doesn't know the C language very well, and that you should find another book.
The story is different for "freestanding" (embedded) systems. For such systems, the program's entry point is entirely implementation-defined, and might not even be called main
. Defining it as void main(void)
may well be valid for such systems.
The 1999 ISO C standard dropped the "implicit int" rule. Taking advantage of that rule was probably never a good idea in the first place. As of ISO C 1990, you could legally use:
main(void) { /* ... */ }
because it was equivalent to:
int main(void) { /* ... */ }
As of the 1999 standard, the int
is mandatory.
The 1999 standard also added a special-case rule: reaching the closing }
of the main
function is equivalent to executing return 0;
. It's still not a bad idea to add the explicit return 0;
, especially if your code might be compiled with a pre-C99 compiler.
The 2011 ISO C standard didn't make any changes in this area.
The difference between int main()
and int main(void)
is that the latter explicitly says that main
takes no arguments; the former doesn't specify how many arguments it takes. Use the int main(void)
form. There have been debates about whether int main()
is even legal.
You can likely get away with writing void main()
, since it's an error that compilers are not actually required to diagnose (it's undefined behavior unless the implementation documents it).
The bottom line: The proper definition of main
has a long and varied history, and there are a lot of variant forms you can probably get away with using. But unless you're programming for an embedded system, there is no point in using anything other than one of the two officially valid forms:
int main(void) { /* ... */ }
int main(int argc, char *argv[]) { /* ... */ }
c automatically implies the datatype int
to functions with no declared datatype. So as far as the compiler is concerned the above is:
int main()
{
printf("hello world\n");
}
This expects that you would return an integer at the end of it with a return
statement. If you explicitly specify it as void main()
you are telling the compiler that the function does not have a return value, hence no warning.
The reason that this is not an error is that if not specified, main()
will return 0;
at the end of execution. However the compiler is still giving you a warning that this is happening.
Best practice is to use int main()
and then return 0
at the end of your program execution like this.
int main()
{
printf("hello world\n");
return 0;
}
See: this question for more information.
You got the warning because you didn't specify the return type of main
.
You should always use int main
, and return an int
number, usually 0
for success.
int main()
{
printf("hello world\n");
return 0; //you can omit this since C99
}
Using void main
on a hosted environment(normally we are, if not, the following doesn't have to be true) leads to undefined behavior, even though it works in some compilers, never use it.
The standard says main
has two kinds of prototype, both returns int
:
C11 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;10) or in some other implementation-defined manner.
write
return 0 ;
at the last line.