Return 0 on Linux and Windows

Asked

Viewed 227 times

5

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

My question is, why the return 0 is necessary on Windows but not on Linux? Has it anything to do with the compiler used? I used both the g++ as to the gcc in the terminal.

1 answer

9


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

Have compiler who can decide that a function always has a return of the implicit specified type if you do not put something. You have a compiler that requires being explicit to avoid exiting the function in an unwanted way. As the specification says nothing about it neither forcing nor forbidding, each does as he wishes.

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

Generally it is more readable and avoids bugs write the return explicitly, but in the case of main() and is something simple I do not see major problems, even more in exercises, I have avoided.

  • My teacher could not explain it to me, I also searched on forums and found nothing relative. I thank you very much for your reply, you have taken away a question I had a long time ago. I’m new around here so I’m sorry if I asked the question improperly.

  • 1

    It’s all right. Try to study from various sources and understand why things are. You’re on the right track by not conforming to "things are like this".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.