int main () expects to return a value as to success or not
So, yes you need to return a value in the return statement for proper compilation to occur.
return 0 says function exited normally with no error making possible other tests after the function exits to know what to do as in
if funct1() {…} else {…}
In principle you can define the main function also as void, this is
explicitly allowed by the C standard. But as long as you run your program on
top of an operating system, which expects to get a status value back and not
as a C program in an embedded system on the bare metal without operating
system (for example a simple microcontroller), it would be bad style to do
that.
–
PC: oS 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.6.2 | GeForce
9600 GT | 4GB Ram
Eee PC 1201n: oS 11.4 64 bit | Intel Atom 330@1.60GHz | KDE 4.6.0 | nVidia
ION | 3GB Ram
Sorry, I probably caused more confusion.
main is a special function and should always be an integer.
I shouldn’t have used it as an example of a void function.
This is a better example, I hope…
#include <stdio.h>
void hello();
int main () {
hello();
return 0;
}
void hello () {
printf ("hello world
");
return;
}
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 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;9) or in some other implementation-defined manner.
It also depends on whether you are talking about C++ or C. Apparently it comes down reading the standard very carefully to discover that void main is not disallowed in C.
Nonetheless, I personally recommend always using int main so that you don’t run into complaints from the compiler and prototype libraries. It’s not hard to put just return 0; as a last executable statement of the function if you can’t think of anything else to do. I think the compiler is also smart enough to know that exit(n) will not return, so that will also do as a last executable statement.
I hope this is just a typo on your part otherwise I have to conclude that you just like putting your foot in it. Do you actually test compile the code before you post I wonder? I do for code I post, and I’ve been coding for years.
FYI in 4 lines you manage to specify void as the return type, then you return 0. That causes a warning. Really. Try it. The other warning you will get is that int main is expected.
It is wrong typo or wrong coding. I have tried to run in Dev C/C++. And I come to correct it.
I thing also to write it without return 0 because it has something to return. When we call the hello() function, the hello() functions has a “Hello World”, so something has return from function to main program.
So it will be better to write in the beginning of program include<stdlib.h> and to the end a system(“pause”), So it will be better for us to see How program is running.
It will be better to write only in printf("Hello World
") without return <something>
As has been mentioned, it’s good practice to return an integer from the
main function, in order to provide some information on the success of
the program.
I would like to point out the following:
On Sat, 7 May 2011, stamostolias wrote:
>> If you have
>>
> --------------------
> int main()
> --------------------
> you should write it as
>
> Code:
> --------------------
> int main(int argc, char *argv])
> --------------------
There is nothing more ‘correct’ about writing the main function with argc
and argv, and actually if your program is not using any command-line
arguments they are doing absolutely nothing but wasting your time to
write them.
Also, as the return type int is implied in C, you could write:
main()
{
puts(“Hello world!”);
return 0;
}
But I prefer to write the return type for every function.
On Sat, 7 May 2011, stamostolias wrote:
> So it will be better to write in the beginning of program
> include<stdlib.h> and to the end a system(“pause”), So it will be better
> for us to see How program is running.
system(“pause”) will not show you how the program is running!!
What extra information do you think your program gives you because you
paused it?
If you really want to watch the details of how the program is running,
step through it in a debugger, I recommend gdb.
Maybe I have not written it well. Sorry for that. The word key who I wanted to use is to “watch the details” the running details( I mean system(“Pause”).
There is nothing more ‘correct’ about writing the main function with argc
and argv, and actually if your program is not using any command-line
arguments they are doing absolutely nothing but wasting your time to
write them.
There are no arguments but a different solution of same problem.
About
int main(int argc, char *argv])
is from compiler. Anyway it will be better to write it.
According to standards, you should return an integer from main.
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;9) or in some other implementation-defined manner.
This is from the ISO/IEC 9899:1999 (C99) standards document.