Solved

int main() returns?

Posted on 2001-06-12
6
197 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
[X]
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
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
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!

 
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

Technology Partners: 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!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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++.

729 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