main() return type convention [was Re: Linux compiling]

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/06/04


Date: Mon, 06 Dec 2004 13:51:03 GMT


"Anand Hariharan" <mailto.anand.hariharan@gmail.com> wrote in message
news:22868227.0412031503.74a1bca7@posting.google.com...
> Alwyn <alwyn@blueyonder.co.uk> wrote in message
news:<pan.2004.12.03.07.51.36.638908@blueyonder.co.uk>...
> > On Thu, 02 Dec 2004 21:04:40 -0600, Jack Klein wrote:
> > >
<SNIP>
>
> What possibly annoys the gurus (like Jack) is that main's
> return type is something so easy to get it right, that they find
> it irksome when folks "refuse" to get it right.
>

I'm not sure folks so much 'refuse' as some just don't know any better. This
is certainly applicable to novices and students who are simply following the
examples provided to them by their teachers, and the authors of various
programming books and tutorials.

In other situations, say professional developers targeting a single platform
with a particular development tool [e.g. Win32 GUI applications using MS
VC6++], standards-conformance may simply not be an issue. Instead, all that
counts for them is that the compiler emit code that works reliably on the
target platform, and whether it is standards-compliant or not, is moot.

According to:

    http://www.eskimo.com/~scs/readings/voidmain.960823.html

it would seem that the practice commenced with professional developers
taking steps to eliminate compiler errors or warnings [thus, inadvertantly
creating incorrect code], and was propogated via authors / teachers using
such incorrect code in their examples.

>
> Sometimes, I speculate, that it might have something to do
> with an antipathy towards Microsoft (VC6 almost enforces
> void main, and MSDN is replete with void main examples)
>

And those code examples are very likely to remain there since 'old code
never dies' :) !

>
> and/or Sun (Java's equivalent of main returns void).
>

Although Java is, syntactically, in the C / C++ family of languages, you
should not assume that idioms / conventions applicable to either of the
former also apply to it [and vice versa].

As regards returning a value to the host operating system:

* It is not performed via a 'return' from Java's 'main' equivalent

* A programmer may specify a return code via the 'System.exit()'
   method [similar to exit() or std::exit()], but this is entirely
   optional - a default value is returned if such a value is
   required

* The Java runtime [JVM] takes care of supplying a default
   return value to the operating system *on systems that require
   it*; a programmer cannot therefore write an application that
   behaves erratically due to a return value issue

Cheers,

Anthony Borla



Relevant Pages

  • Re: HOWTO Validate security privileges
    ... part of the operating system". ... LUID *pLUID; ... cmstpPrivileges.SetAt("Act as part of the operating system", (void *) ... bRet = OpenProcessToken ...
    (microsoft.public.platformsdk.security)
  • Re: xmalloc
    ... void *xmalloc ... and libraries are where malloc calls belong. ... Therefore, the programmer won't call ... On the other hand you could argue that xmallocis a bad idea, because no library that uses it could ever find its way into a life-support machine. ...
    (comp.lang.c)
  • Re: RPN maps to basic mathematic expressions nicely
    ... A novice C programmer may have problems understanding this, but it's actually quite idiomatic for embedded systems programmers. ... void delay ... A C programmer could also do this at compile time with some macros, but probably more common would be to deal with this with a run-time interpreter for a "language" of dits and dahs: ... If your primary language is C, you're likely looking at the Forth code and thinking that the SWAP and verbose syntax for ORing-in a 0x01 says you're dealing with a language one step above assembler. ...
    (comp.lang.forth)
  • Re: How to install Java compiler and configure it
    ... You can get Java SDK 1.4 from Sun: ... > programmer should use linux like os insted of using Windows platform? ... The programmer likes a cheap and stable operating system. ...
    (comp.os.linux.misc)
  • Re: mixing C and assembly
    ... the assembly programmer always has some ... by a bar call, but that bar needs to be separately callable. ... void bar; ...
    (comp.arch.embedded)