1

In my book on C, the author usually formats main() like this:

main()
{
     /* Some code */
     return;
}

But sometimes, he formats main() like this:

main()
{
     /* Some code */
     return 0;
}

What's the difference? Does it matter?

Yu Hao
  • 111,229
  • 40
  • 211
  • 267
  • 3
    If he's declaring `main()` rather than `int main(int argc, char** argv)`, the book probably isn't written to follow best-practices as-is. – Charles Duffy Feb 16 '14 at 15:48
  • return 0; means program exited successful and atleast in (unix) while return just terminates the program regardles of it's state. in void functions, it it's an exit point – Zuko Feb 16 '14 at 15:50
  • Tell us what book it is so we can make sure no one buys it, it is teaching non-standard practices. –  Feb 16 '14 at 16:02
  • I'm using _C By Example_. It's from somewhere in the 80's, so I wouldn't be surprised if standards have changed. It doesn't even talk about GUI's or graphics. – Daniel Stevenson Feb 16 '14 at 17:01
  • Old versions of `C` permitted declaring `main` like this. Never write it in new code. (A rare exception would be that the C standard permits implementations to specify acceptable alternative declarations of `main`, and an embedded C implementation might do this.) – Eric Postpischil Feb 16 '14 at 17:06

3 Answers3

4

C standard says that (draft n1570)

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.

Now, if a function has return type other than void then you have to use

return exp;

if its return type is void then you can use (not necessary)

return;  

In case of main you can use return 0; but C99 allow you to omit the return statement.

haccks
  • 97,141
  • 23
  • 153
  • 244
  • That explains a lot. My book is sort of old, and I guess `main()` didn't have to be type `int` when it was written. – Daniel Stevenson Feb 16 '14 at 17:09
  • The compiler like **Turbo C++** (which is very old and obsolete) supports `void main` definition. In older C, if the return type of a function were missing then it implicitly converted to `int`. But C99 and latter drops this feature. – haccks Feb 16 '14 at 17:14
2

First, main should be declared with:

int main(void)

or

int main(int argc, char *argv[]) 

The version with command arguments has some variants, like int main(int argc, char **argv)

It must return int to conform the standard. And because of that, you must return an int at the end, return 0 if the program runs normally.

Note that in C99 or above, you can omit return 0. If main is executed to the end, return 0 is implicit.

Yu Hao
  • 111,229
  • 40
  • 211
  • 267
0

The return value indicates (un)successful completion to the environment, allowing said environment to monitor it instead of parsing printed messages. - see this question.

You should use return 0 in main, with a properly declared main function. In functions declared as void, you can just return without a value, or let it fall off the end (no return statement).

Community
  • 1
  • 1
kbshimmyo
  • 578
  • 4
  • 13