2

Possible Duplicate:
return 0 implicit
Why is return 0 optional?

why does the main() function in C can terminate correctly even without using exit or return?

for example:

#include<stdio.h>

int sum(int a,int b)
{
 return (a + b);
}

int main()
{
 int a=10;
 int b=5;
 int ans;    
 ans=sum(a,b);
 printf("sum is %d",ans);
}
Community
  • 1
  • 1
freeboy1015
  • 2,227
  • 2
  • 18
  • 15
  • @freeboy1015 Just ad Ed noted, if your questions have been answered with what you needed, please click on the check mark next to the most suitable answer. You can view your older questions by clicking on your profile and then going to Questions. – Fingolfin Nov 05 '12 at 13:15

5 Answers5

15

It's because the C99 and C11 standards says so:

5.1.2.2.3 Program termination

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.

However, you should add a return statement. This is because the returned value is undefined in C89 otherwise!

3.6.6.4 The return statement

Reaching the } that terminates a function is equivalent to executing a return statement without an expression.

2.1.2.2 Hosted environment

If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined.

2501
  • 25,460
  • 4
  • 47
  • 87
orlp
  • 112,504
  • 36
  • 218
  • 315
11

Because the compiler adds an implicit exit for you.

Check nightcracker on why; still I strongly recommend you make an explicit exit/return with a meaningful return code.

C99 standard:

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;10) 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.

I am looking for the C89 text to check.

Fingolfin
  • 5,363
  • 6
  • 45
  • 66
  • C89 and C99 are different here, aren't they? – md5 Nov 05 '12 at 13:20
  • 1
    @Kirilenko Yes, in C89, you _must_ have a `return` (I'm not even sure whether an `exit` instead of the `return` was compliant then). On the other hand, in C99 you _must_ declare the return _type_ of `main`. – Daniel Fischer Nov 05 '12 at 13:27
0

Because if the execution terminates without either it assumes it is successful i.e. returns 0.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

Primary, your compilator will not tell you anything if you don't use Flags.

If you use severe flags, your compilator will tell you every little missing detail.

Returning something is important, the program which started your can be informed on the succeful or not execution of your program. If your program return nothing (so 0 by default), the terminal in which you started it, for instance, will assume that it worked perfectly even if it failed.

Weacked
  • 954
  • 2
  • 7
  • 18
0

Because the compiler adds it for you.

On unix/linux you can try this little script:

retval.sh

#!/bin/sh
$1
echo $?

Then run your program through it:

./retval.sh yourprogram

It will output the return value of your program. You will see main() even if you add no return or exit() will return an integer value.

iagorubio
  • 111
  • 6