Solved

int main() returns?

Posted on 2001-06-12
6
195 Views
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?
0
Comment
Question by:Subhuman
6 Comments
 
LVL 5

Accepted Solution

by:
robpitt earned 50 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.
0
 
LVL 5

Expert Comment

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

EXIT STATUS
     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
0
 
LVL 7

Expert Comment

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

3.6.1:
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'
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 5

Expert Comment

by:proskig
ID: 6182096
KangaRoo:

3.6.1.

-5-
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 ;-))
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6182187
:-\
0
 
LVL 1

Author Comment

by:Subhuman
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

829 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