13

In C++, 3.6.1 Main function

(3.6.1/5) A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

Can I do the following in C99 without return 0?

int main() { }
user963241
  • 6,758
  • 19
  • 65
  • 93
  • 8
    You have a C++ standard but not a C standard? – Carl Norum Nov 24 '12 at 20:31
  • 1
    @CarlNorum: Don't they both cost money? – NPE Nov 24 '12 at 20:32
  • @NPE: C99 Standard is freely avalaible online. And possible duplicate that also answers the question: http://stackoverflow.com/q/204476/1202636 – effeffe Nov 24 '12 at 20:33
  • Drafts are free. Google "C99 PDF", and it's the first hit. – Carl Norum Nov 24 '12 at 20:33
  • @NPE: the released standard costs money in both cases (at least for a legitimate copy). In either case, drafts almost indistinguishable from the real standard are available for free. – Jerry Coffin Nov 24 '12 at 20:41
  • @NPE: actually I can easily find free PDFs of all major revisions of C and C++ standard except for C89 (just found a plain ASCII draft...), sometimes they are draft, but they are the same as the real standard. – effeffe Nov 24 '12 at 20:43

2 Answers2

16

Yes, as of C99, reaching the } at the end of main returns 0 if the return type of main is compatible with int.

5.1.2.2.3 Program termination

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
9

Yes, the C99 standard says (§5.1.2.2.3):

reaching the } that terminates the main function returns a value of 0.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324