Just as it is possible to pass arguments to the code by the function main()
through the parameter argv
(with the help of argc
) And this data comes from whoever called the executable, you can also return a data to who called it. Usually who called is the operating system (by action of some user or script that takes care of it) and it is he who will receive this information to be used in some way, for example a script can take this and decide what to do.
The number that will return is defined by yourself for your application and should document this well so that whoever is calling it can use this result for something according to the semantics that you wanted to give and that makes some sense. In general each number indicates some specific error it gave, according to its documentation.
But it is a mistake that you need to use the return 0
, if you don’t do this there will be an implicit return of 0, at least this is true for all the best known compilers.
And there is another detail, almost every time you return an error you will do it through, directly or indirectly, the function exit()
or something similar, since this will occur in another part of the code than the main()
and shall close immediately.
IS standard in C11/C18 need not return value in
main
?– Mateus -- O Schroeder
Yeah, as far as I’ve known since C99.
– Maniero
I found in 5.1.2.2.3 of the draft "reaching the } that Terminates the main Function Returns a value of 0."
– Mateus -- O Schroeder