2

Which is a good practice of int main() declaration in C?

int main(){
stuff;
return 0;
}

or

int main(){
stuff;
}

I have tried searching on the internet and most of them are unclear with some mentioning about compiler stuff. I know that a function should return something. However, both works perfectly normal on my computer. Any help on this topic will be greatly appreciated.

spyC
  • 45
  • 9
  • 1
    "I know that a function should return something". No it doesn't have too.. There is nothing making it forced to return something, hence why we have void returning functions – Omid CompSCI Jun 24 '19 at 04:19
  • These are not declarations but specifically *definitions*. `int main();` is a declaration, and yes, it should have the `int` there. – Antti Haapala Jun 24 '19 at 04:33
  • @AnttiHaapala I've added the canonical dupe [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) (from the [C wiki FAQ](https://stackoverflow.com/tags/c/info)) to the dupe list. I believe we should use that one as dupe target for "format of main()" questions. – Lundin Jun 24 '19 at 07:13

3 Answers3

4

Both will work, because main is special. There's no difference in the compiled code. The standard guarantees that main returns 0 if it terminates without an explicit return value.

But it's better to be explicit and return 0.


For skeptics, an excerpt from the C standard clause 5.1.2.2.3 (Program termination):

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; reaching the } that terminates the main function returns a value of 0.

rici
  • 201,785
  • 23
  • 193
  • 283
  • 2
    Yes, simple but good answer. Although I'm surprised why modern compilers don't actually give compiler errors (if warnings are turned on), if main doesn't return something and you have int as the return type. It complains for all other functions if that is done. – Omid CompSCI Jun 24 '19 at 04:22
  • Hello "The standard guarantees that main returns 0". Can you provide a source for this if it is possible? This holds for GCC afaik. MSVC complains when main does not return anything if I remember correctly. Thank you in advance. – Sıddık Açıl Jun 24 '19 at 04:28
  • @OmidCompSCI I believe that's mostly because other functions will vary in nature and the tasks they perform. That's why there's a restriction on them. However when it comes to `main()`, it has only 2 major roles : First, to serve as a starting point & second, to inform the system that the program has executed successfully with the help of `return 0` in the end. – Argon Jun 24 '19 at 04:30
  • @SıddıkAçıl: This behavior has been mandated since C99. Microsoft has been very slow to upgrade its compiler to support C99, which hampered the adoption of new language features (among other reasons). Regarding the rationale and references, see this question: https://stackoverflow.com/questions/31394171/what-was-the-rationale-for-making-return-0-at-the-end-of-main-optional – chqrlie Jun 24 '19 at 04:49
  • @SıddıkAçıl: At your service. – rici Jun 24 '19 at 04:51
  • @omid: as I said, `main` is special. And the behaviour is clearly specified, and is reasonably well known. Triggering errors on valid programs is considered unfriendly; you need to consider the gazillions of lines of legacy code in the world which still get recompiled when a distribution is built, for examples. – rici Jun 24 '19 at 04:55
4

Because many programmers used the second style, causing unspecified exit status to be reported to the system, the C Standard committee decided to make main return 0 implicitly if control leaves its body without a return statement. This behavior is mandated by the C Standard C99 and later versions. As a consequence, return 0; can be omitted by it is better IMHO to still make it explicit.

Note however that it is also considered good style to indent the statements in the body of functions:

int main() {
    stuff;
    return 0;
}

Note also that the C Standard documents 2 possible prototypes for main:

int main(void);

and

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

or equivalent variants:

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

etc.

Omitting the argument list as you wrote in both examples is supported and would be equivalent to int main(void) in C++, but is not exactly equivalent in C: It means the argument list is unspecified, so the compiler cannot check the arguments passed to main if it encounters a call to main in the program, not can it perform the appropriate conversions.

In this case, it does not matter since the main functions in the examples do not use their arguments and indeed seems more consistent than int main(void) since arguments are indeed passed to it by the startup code.

chqrlie
  • 98,886
  • 10
  • 89
  • 149
0

Normally it is not allowed for the control flow to reach the end of a non-void function without returning something. The main function is handled differently.Refer the below document page 59 & 62

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2960.pdf:

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

Gopika BG
  • 852
  • 6
  • 11
  • 1
    The document in reference is the C++ Standard, which is not always relevant for a C question. – chqrlie Jun 24 '19 at 04:39