7

In most cases int main() does not return anything, it doesn't even have to since no return would not give an error. So why does main have to return an int? Why is void main not possible?

EDIT: I meant, why is int main() the standard if there usually is no return?

user2180680
  • 209
  • 2
  • 6
  • 1
    because there's an implicit return in main even when you don't write it. – Karoly Horvath May 17 '13 at 13:56
  • Here int contains two value either 0 or 1. 0 means your program successfully executed ,where 1 means termination of program with some error. And this value is returned to OS. – Sudesh Kumar May 17 '13 at 13:59
  • @SudeshKumar, You can return numbers other than 0 or 1... – chris May 17 '13 at 14:26
  • 1
    @SudeshKumar - the only number you can return is 0. There are two manifest constants that you can return: `EXIT_SUCCESS` and `EXIT_FAILURE`; the language definition doesn't say what the values of those constants are. – Pete Becker May 17 '13 at 15:46
  • @chirs you are right :) even i'm also tested with character. Beacuse character also converted into numeric value. – Sudesh Kumar May 18 '13 at 07:00

3 Answers3

13

Other programs may use the return code to determine whether the application executed successfully. Zero typically means successful execution.

Matthew
  • 24,703
  • 9
  • 76
  • 110
7

void is possible but non-standard. The returned int is meant to signify something for the caller.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
4

The same way as a function in your program may returns values to indicate its result, main returns to indicate the result of execution of your program.

Also in case int main() does return explicit, compiler will put return 0 automatically

Andrew
  • 24,218
  • 13
  • 61
  • 90