int main() returns?

Posted on 2001-06-12
Medium Priority
Last Modified: 2010-04-02
Looking through the documentation for my compiler, it says that there are special meanings (in DOS) for values returned by main(), but it doesn't actually specify what means what.

What are these return values that DOS recognises, and what do they mean?
Question by:Subhuman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

robpitt earned 200 total points
ID: 6180452
The exit code from a console mode program (or DOS program) is put into the errorlevel value in the shell which executed the program.

The exact meaning of the exit-code/errorlevel is program specific. However the convention is to use "0" for success or 1..255 for custom error codes.

Expert Comment

ID: 6180530
There are actually two approaches. First is platform-specific, just an example from man page, what does UNIX grep returns.

     The following exit values are returned:

     0              One or more matches were found.
     1              No matches were found.
     2              Syntax errors or inaccessible files (even  if
                    matches were found).

So if you really need to differentiate between error codes, you can try to find out similar commands/programs and check what do they return.

Now, second part (and it would be good, if it is enough for you) is C++-laguage specific.
The standard requires cstdlib header files to contain two macros EXIT_SUCCESS and EXIT_FAILURE. So, maybe the easiest way for you is to return EXIT_FAILURE in case of any error, note also if your program does not return anything, it is equivalent to
return 0; // EXIT_SUCCESS
MSVC produces erroneous warning in this case.

Hope it helps

Expert Comment

ID: 6180989
>> erroneous warning
Indeed, main /must/ return an integer so it should be an error message :)

2 An implementation shall not predefine the main function.   This  func-
  tion  shall  not  be  overloaded.  It shall have a return type of type
  int, but otherwise its type is implementation-defined.  

>> special meanings (in DOS) for values returned by main(), but it
>> doesn't actually specify what means what

There are no standardized meanings, though errorlevel 0 is indeed often used as 'no-error-occured'
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6182096


A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;


Sometimes it is useful to read till the end ;-))

Expert Comment

ID: 6182187

Author Comment

ID: 6189088
Borland compilers allow you to compile the main method with a return type of void, as well, although I tend to avoid this now.

Thanks for the feedback folks, I'll grade this at home later.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question