Solved

int main() returns?

Posted on 2001-06-12
6
193 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now