Return 0 on Linux and Windows

4

I'm starting the ADS course and my programming teacher insists on using return 0 at the end of the function. Most use Windows, I realized that return 0 is required in Windows but in Ubuntu (which is what I use) I do not put return 0 but anyway the program runs normally without any errors.

My question is, why does return 0 become necessary in Windows but not in Linux? Does it have something to do with the compiler? I used both g++ and gcc on the terminal.

    
asked by anonymous 01.09.2017 / 04:57

1 answer

8

This has nothing to do with the operating system, it has to do with the compiler. You are probably using Microsoft VC ++ on Windows and GCC on Linux. In fact it may even depend on configuration.

There is a compiler that can decide that a function always has a return of the specified type implicit if it does not add anything. It has compiler that requires being explicit to avoid leaving the function in an undesired way. As the specification does not say anything about it neither obliging nor forbidding, each does as he wants.

This holds for any function, the fact that it is in main() is just a coincidence. So as main() was declared as int return, there will always be a return of 0 if nothing is returned explicitly. Note that main() does not even need to be called by the operating system, it is a common function.

In general it is more readable and prevents bugs writing the return explicitly, but in the case of main() and is something simple I do not see bigger problems,     

01.09.2017 / 05:08