Solved

int main() returns?

Posted on 2001-06-12
6
194 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

810 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