At the suggestion of the PaperBirdMaster user, I have decided to ask the question indicated in the title of this post.
Basically, as I mentioned to said user, I have seen in different SO answers that in C++ it is not necessary to include the instruction return 0;
in the function main
since the compiler is capable of including said instruction on its own. How much truth is there in the above statement?
Personally, I prefer to include the return 0;
because it goes along the lines of using a return
within a function, since by definition functions return a value. It would be different if main
he declared it as void main()
, which is not standard, but at least it is valid in Visual Studio, for example, and there he would see the need to include some kind of return
.
On the other hand, related to all of the above, how valid is it to use another type of return
like, for example, return EXIT_SUCCESS;
, which can be found in one of the books on C++ by Luis Joyanes.
Thanks in advance for your responses and/or comments.
Looking at the C++14 draft , in section 3.6.1 we find the following:
Which comes to say the following:
So the answer is NO , it is not mandatory to include the statement
return
in the main function .Yes, it will be mandatory to add this instruction when the application needs to return different values depending on the execution result (for example, to indicate errors).
If we read section 2 of the aforementioned section (3.6.1):
Which basically means that the main function must return a type
int
. Of course, for compatibility reasons, there are compilers that swallow withvoid
... but be careful with this because nothing guarantees that the next version of the compiler will accept both that and many other licenses ... you may find yourself in the situation of having to edit dozens of lines of code just to have the program recompile.EXIT_SUCCESS
is not a type but is a constant:And it's available for both C and C++ via the
stdlib.h
orcstdlib
.Therefore,
EXIT_SUCCESS
it is, in your case, equivalent to puttingreturn 0;
.So why use
EXIT_SUCCESS
if you write more?Well, keep in mind that both C and C++ have not been designed for a specific architecture... if you find yourself with an Operating System in which an application must return a value other than 0 when it finishes correctly, the macro will adapt without problems while he
0
will have to modify it by hand.True, with nuances.
main
, whose behavior is defined.return
except in the case ofmain
, but it does so to follow the standard, not because it is a free choice of the compiler.In the C++ standard the relevant text on the first point is (translation and emphasis mine):
The relevant text on the second point is (translation and emphasis mine):
Conclusion.
While it is true that a C++ compiler following the standard behaves in a defined way on functions
main
without a statementreturn
, I (personally) object to it for the following reasons:main
had other explicit returns, it would be even more disturbing to rely on onereturn
implicit while the others have not been 1 .A comment could be added at the end of
main
to show that intent:But it's much easier to just write the
return
appropriate statement!Explaining this topic of the statement " return 0; "; What this does is terminate a function and return control to where the function was called. For example: In the case of a simple C++ program that writes "Hello World"; what this instruction does is end " main " and return control to our machine's operating system; the value 0 tells the operating system that the program has finished successfully. According to the latest version of C++; this return 0 can be omitted and the program will work normally!
I understand the arguments and good programming practices. But going out of convention you could return a 99 and it would only make sense to you, manipulating the main from some console and accessing that return value.