Main method can be of type void too!

I just changed the return type of main method from int to void
so why dont we use void since we dont want to return anything, we just wanted to execute the program like in C# where the main method returns void ?

It may work for you at that moment and at that time and on that OS. That doesn’t make it 100% working in all cases.

It also completely avoids the whole official standard of returning an int correctly (possible that the compiler is doing it for you anyway).

You might be better served by learning why one would want to return something other than 0, when returning something else should be done, and how that works since that isn’t really taught at the point you’re at to the extent that you’re changing things around at. That should hopefully help you to understand why returning 0 should be.

If no return value is specified in main , the compiler supplies a return value of zero.

As a Microsoft extension, the main and wmain functions can be declared as returning void (no return value). This extension is also available in some other compilers, but its use isn’t recommended.

If you declare main or wmain as returning void , you can’t return an exit code to the parent process or the operating system by using a return statement. To return an exit code when main or wmain is declared as void , you must use the exit function.

1 Like

If you want to read standardese, the standard only specifies two signatures. Anything else is a compiler extension:

https://eel.is/c++draft/basic.start.main#2

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms