I have been advised by many people that it is better to use integer return type for main function instead of void ! I actually dont understand what return 0 means .. main returns 0 value to program ! What does that mean ?! Could you please help me understand the concept?

  void main()
  {
      body
  }

  int main()
  {
      body

      return 0;
  }

Edited 3 Years Ago by pritaeas: fixed formatting

The C and C++ standards dictate how compilers do things. And the standards say that main() must return an integer value to the operating system. That always happens even in you decide to declare main() return void or even something else such as char*.

One reason for that is to let the operating system and possibly other programs know the exit status of the program. Its customary for return value of 0 to indicate that the program is exiting normally. Any other value would indicate some sort of error condition.

This link will help you:

http://www2.research.att.com/~bs/bs_faq2.html

Read the answer to " Can I write "void main()"? " question.

In short words:
1. "[void main()] is not and never has been C++, nor has it even been C."
2. returning "0" means "successful execution"
3. "Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers." (Bjarne Stroustrup)

Edited 6 Years Ago by hystaspes: n/a

There are macros defined in the standard libraries, they are "EXIT_SUCCESS" and "EXIT_FAILURE". Find those macros and see what their values are. That may help you understand.

Sometimes you'll see "return EXIT_SUCCESS;" at the end of a piece of code. This is exactly the same as putting "return 0;" at the end. This is because the value of the macro "EXIT_SUCCESS" is #defined as 0. It's just easier to type 0 instead of EXIT_SUCCESS.

Edited 6 Years Ago by Fbody: n/a

It's just easier to type 0 instead of EXIT_SUCCESS.

Also it is better to avoid macros in c++, even if they are defined in the standard c++ library (unless we have no other solution).

but even if there are some errors in the program it will still work with those ever no matter if there is a return 0 ! isnt that so? and will the compiler be able to detect an error of an infinite loop if return 0 is present? so , it wont return 0 if there is an error which prevents the exiting of the problem? I dont understand properly ! please explain

once the main function is done it will anyway return 0 , right? !! Then what is the purpose of a return 0 ? what if there is an error which prevents the exiting of the program , if that is the case , what value be returned instead of 0?

so? How will that help me? ! the program will return a value 0 if it ends successfully ! but if I use void , it will still end successfully but wont return a value! whats the purpose of that ? you wont need to check if the return value is non zero to figure out that it is not extiting !!!

Why are you making this so complicated? If the program ends normally (without error) it sends a 0 back to the calling environment to indicate zero errors. What is so hard to understand about that? You're trying to link things that are unrelated.

Using void as a return value is non-standard, some compilers allow it, but shouldn't. Even if you do get away with using it, most compilers put code in that returns 0 for you.

In the case of an infinite loop, that is a programming error (meaning it is YOUR fault), not a program execution error. When in an infinite loop, the program is still running. If the program is still running, it hasn't yet generated a return value, and will not.

Just accept the fact that you need to do it this way and be done with it.

Edited 6 Years Ago by Fbody: n/a

as far as I know (please correct me if i am wrong), your program is called by another process (it can be called by your OS or by another program), IF your program has done -what you have written- with no errors (please note that it doesnt necessarily mean that your program is logically correct), it will reach the end of the main function and returns a "0" and tell the caller that "i did my job"!

if you implement error handling in your code, you can return other integers to the caller in case of any errors. for example your program returns "1" to its caller if some kind of error happened during run-time, this way you are telling the caller that something is not right. based on the returned value by your main function the caller can decide what to do with error number "1".

may be you don't need to return a "0" to any other process right now, but that doesnt mean you wont need it in the future when you are working as a professional programmer on a real project in a team, working on tens or hundreds of thousands lines of C++ code, working with multiple threads and processes etc.

in my opinion, the most important thing is to follow the standards. C++ standards tells you that you cant use "void main()", you cant "use [variable] names with all capital letter (e.g., BEGIN_TRANSACTION) because that's conventionally reserved for macros", you should never use macros unless you have no other way, you should avoid declaring and using global variables and so many other things.

>>it will reach the end of the main function and returns a "0" and tell the caller that "i did my job"!

Only if you do not specify a return value or you specifically return 0 in the return value. There is nothing to say that main() can not return something else even if it did successfully processess everything. The compiler itself could care less what main() returns as long as its an integer.

As one example of how the return value might be used, I wrote one program many years ago whose return value was used in a batch file to determine what actions to take. The C program had a menu of items from which to choose, and returned from main() the chosen menu item number.

Edited 6 Years Ago by Ancient Dragon: n/a

The simplest way to get that value is to use the return value of the sytem() function. See the example here.

A slightly more complex example is to launch the new process with CreateProcess() win32 api function, WaitForSingleObject() to wait until that preocess terminates, then call GetExitCodeProcess() to retrieve the return value from main(). This is more complicated than using system() but gives you a lot more control over the newly created process.

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.