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 themain()
function's return type. That means that the kind of valuemain()
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 argumentswrite like this :
You might wanna look at the C99 standard for further details.
c automatically implies the datatype
int
to functions with no declared datatype. So as far as the compiler is concerned the above is:This expects that you would return an integer at the end of it with a
return
statement. If you explicitly specify it asvoid 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()
willreturn 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 thenreturn 0
at the end of your program execution like this.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 anint
number, usually0
for success.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 returnsint
:C11 5.1.2.2.1 Program startup
Quick summary: If you don't want to use command-line arguments, you should write:
If you do:
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. Returning0
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:
Or, if you wanted to use command-line arguments:
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:or
(
char *argv[]
can also be written aschar **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()
orvoid 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 thevoid
keyword simultaneously established the rule thatmain
's return type isint
.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 asvoid 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:
because it was equivalent to:
As of the 1999 standard, the
int
is mandatory.The 1999 standard also added a special-case rule: reaching the closing
}
of themain
function is equivalent to executingreturn 0;
. It's still not a bad idea to add the explicitreturn 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()
andint main(void)
is that the latter explicitly says thatmain
takes no arguments; the former doesn't specify how many arguments it takes. Use theint main(void)
form. There have been debates about whetherint 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:write
at the last line.