Answer the question
In order to leave comments, you need to log in
Is return in main important?
In cpp, as you know, the program starts in the main, and if I specify the type int to it, is it necessary to write return 0 at the end? Well, why am I asking. I'm sitting in a book on the pros and reading, here in each example at the end it is necessary to return 0. Well, of course I tried without it, everything works. But maybe there's something I don't know. Therefore, I would like to ask knowledgeable people whether it is important to return at the end of the main? Well, or stupidly you can specify void and score? How more correctly?
Answer the question
In order to leave comments, you need to log in
Formally, the only function in which you can not do a return is exactly main .
In other cases, if the function returns something, the presence of return is mandatory - otherwise it will be UB.
Why there is an exception for main - no one knows, because the paragraph in the language standard to which another paragraph refers is lost:
I looked it up. I believe this was introduced with C99, and apparently the C99 rationale is defect. It has comments for 5.1.2.2.1 Program startup, then labels the next chapter 5.1.2.3 Program execution. It should have been 5.1.2.2. As a consequence of this, the rationale for Program termination that should have been in the real chapter 5.1.2.3, has gone missing in action. Thus, main allows no return code in C99 and there exists no rational reason why.
The C programming language allows programs exiting or returning from the main function to signal success or failure by returning an integer, or returning the macros EXIT_SUCCESS and EXIT_FAILURE. On Unix-like systems these are equal to 0 and 1 respectively.[3] AC program may also use the exit() function specifying the integer status or exit macro as the first parameter.
The return value from main is passed to the exit function, which for values zero, EXIT_SUCCESS or EXIT_FAILURE may translate it to “an implementation defined form” of successful termination or unsuccessful termination.
Apart from zero and the macros EXIT_SUCCESS and EXIT_FAILURE, the C standard does not define the meaning of return codes. Rules for the use of return codes vary on different platforms (see the platform-specific sections).
If you write code that meets the language standard, then it is correct, of course, to indicate return.
In most modern operating systems, any running process returns a return code. This is an OS requirement. The return code of a process is usually the value returned from main.
But, if the return code is not needed, then you can omit return and declare . Then it is considered that the return code from the program is zero. Usually this works out fine. But this is not a standard feature and your program may not build on some platform and / or compiler. void main()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question