Value returned by main() ?

Hi Guys,

i Was programming from a couple of years but i wasnt aware of where to see the value returned by main()

yesterday i got an answer to this so thought of sharing it.

go to cmd prompt and type

echo %ERRORLEVEL%

to see the value returned by your main()

--Cheers
It's an error for main not to return int in C++; and probably C too but it's not enforced in C and you get some random value returned to the calling program.
In C I think main can return void.
In C++, GCC won't let main return anything but int.
0 is returned for success. I use -x for an exception and +x for user error. But sometimes you need main to return a particular value so that you can get it in another program, in which case you can return what you want.
In both C and C++, main() must return int. That is the standard.

Some compiler$ have traditionally allowed you to write "void main", but that is non-standard. (I think MS compilers will automatically return zero for you when you use "void main", but I'm not very certain about that, and YRMV with other compilers.)

Return values should, for maximum portability, always fall within the range 0..127. Returning negative values is a very bad idea -- it can conflict with additional status information bundled in the return code by the OS.

In C++, it is acceptable to leave off the return statement. The compiler will automatically add a return 0; for you. However, this is a special case -- in other words, a weird syntax deviation from all other functions, so I always just put it in just to be explicit.


Personally, I don't think it is too much of a hardship to write one of:

1
2
3
4
5
int main( int argc, char** argv )
  {
  ...
  return 0;
  }
1
2
3
4
5
int main()
  {
  ...
  return 0;
  }

for every program I write. It is part of the standard. We follow the standard religiously to do other things, like writing assignments, overloading operators, class definitions, etc. Why should main()'s syntax be so hotly hated? Such heat on the topic has always perplexed me...

My $0.02.
I agree.
I didn't know about returning negatives. I'll keep that to other functions.

My 0.012098.
In both C and C++, main() must return int. That is the standard.


The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int. But the ISO C Standard (ISO/IEC 9899:1999) actually does not. This comes as a surprise to many people. But despite what many documents say, including the Usenet comp.lang.c FAQ document (at great length), the actual text of the C Standard allows for main returning other types.

Source: http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html
Mr. Pollard is splitting hairs.

The standard specifically says that main() should return int, else it has an implementation defined variation -- returning an "unspecified" value.

ISO/IEC 9899:TC2 5.1.2.2.1 Program startup Subsection 1:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent9); or in some other implementation-defined manner.

9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
ISO/IEC 9899:TC2 5.1.2.2.3 Program termination Subsection 1:
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.


Now, the reason Mr. Pollard is splitting hairs isn't to give "void main()"ers a lifeline. It is because he knows returning anything but an integer is bad, and he wishes the standard to be amended to fix that error:
http://homepages.tesco.net/J.deBoynePollard/Proposals/c-hosted-program-startup.html

Why is anything but "int main(...)" bad?
http://www.eskimo.com/~scs/readings/voidmain.960823.html
http://users.aber.ac.uk/auj/voidmain.shtml
http://home.att.net/~jackklein/ctips01.html#int_main

Here is a more succinct version of the above three:
http://www.safercode.com/blog/2008/10/14/int-main-vs-void-main.html


The C and C++ standards were written to accomodate the Unix operating system, which has had a more profound impact on OS design than any other in existence. According to the current POSIX standards (and Microsoft Windows, which follows the Unix program status code pattern), a program is expected to return an integer value to its caller. This translates to "int main".

POSIX requires the entry point to a binary executable return int.

So ultimately, if you want portable and standards-correct code, use int main(...).
Topic archived. No new replies allowed.