3

Why is adding a return statement for the main() function in C important if the programs I make runs the same without it?

Is there a disadvantage in my programs if I omitted the return statement in the main() function?

  • 2
    you might wanna check this out: http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c – Yohanes Khosiawan 许先汉 May 21 '14 at 05:16
  • It's only important on SO ;-) (unless you want the prog to return a value to the caller which is commonly done on unix). – Peter - Reinstate Monica May 21 '14 at 05:18
  • @const not really, that question is for C++ and this question is in C. I am not asking about the difference of `return` and `exit()`, I am just asking why I need to specify `return` in the `main()` function. –  May 21 '14 at 05:34

3 Answers3

2

It is by convention that you tell the operating system if your program exited successfully(returning 0), or if there was an error(for example, by returning an error code).

To remain more standards compliant, use return EXIT_SUCCESS or return EXIT_FAILURE.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Let's say that there two types of errors I want the computer to know, is it possible to `return 1;` to specify one error and `return 2;` to specify another error? –  May 21 '14 at 05:24
  • 1
    yeah, conventionally you can do that. and then get the error code returned to the operating system. – Aniket Inge May 21 '14 at 05:25
  • And you can customize actions to take in case a particular type of error occurs, and perhaps re-run the program after correcting the error. – Aniket Inge May 21 '14 at 05:26
1

If you want your code to be legal (i.e. works on all compilers that support the standard), then if you've defined main as:

int main() {
   return 0;
}

It should return an integer. In this case, 0. Why? Because according to the standard, if you've defined a function to have a certain return type, it should return that return type. Otherwise, it just isn't legal.

It may compile (thanks to forgiving compilers, and hence this should not be relied upon), but that doesn't mean it's correct.

jrd1
  • 10,358
  • 4
  • 34
  • 51
  • In C99, It __must__ compile for any standards-compliant compiler. – Bryan Chen May 21 '14 at 05:28
  • I up-voted your answer because of the last paragraph. –  May 21 '14 at 05:29
  • @exilonblack and unfortunately it was wrong in C99 – Bryan Chen May 21 '14 at 05:30
  • @Bryan Chen you mean the "It may compile (thanks to forgiving compilers, and hence this should not be relied upon), but that doesn't mean it's correct." paragraph? –  May 21 '14 at 05:36
  • 1
    @exilonblack Yes. It compiles not because compilers are kind to you. But because the program is valid and should compile. you can rely on this behaviour (although I won't recommended it) – Bryan Chen May 21 '14 at 05:41
  • 1
    I do know that they're not kind, they don't have emotions. After reading all of these comments and replies, I decided to add `return` statements to make programs less problematic in the future. –  May 21 '14 at 05:44
0

The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. It has an explicit "shall" constraint upon well-formed programs. It shall have a return type of int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:

int main() { /* … */ }
int main(int argc, char* argv[]) { /* … */ }

But the ISO C Standard (ISO/IEC 9899:1999) actually does not mandate this as the C++ standard does. This comes as a surprise to many people. But despite what many documents say, including the Usenet comp.lang.c FAQ document (at great length), the actual text of the C Standard allows for main returning other types.

You can use either the int or void. Though, it is better to use the int returning because you should inform the OS how your program exited. See a practical example:

int main() {
  int a;
  printf("Enter a positive number: "); scanf("%d", &a);
  if(a<0) return 1;
  return 0;
}

I am not sure if it is the best example, but it could help you understand the concept better. Hope that helps!

Victor
  • 13,914
  • 19
  • 78
  • 147