www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/04/20:47:57

Xref: news2.mv.net comp.os.msdos.djgpp:8272
From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: HELP MEEEEEE!!!!
Date: Wed, 04 Sep 1996 18:43:22 -0700
Organization: Three pounds of chaos and a pinch of salt
Lines: 98
Message-ID: <322E303A.114@cs.com>
References: <9609041643 DOT aa11427 AT ailin DOT inti DOT edu DOT ar>
NNTP-Posting-Host: ppp211.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Salvador Eduardo Tropea (SET) wrote:
> 
> In answer to the following mail
> 
> >I'll say it again just to be sure you heard me.  "main()" must be declared
> >"int"!!!!!!!!!!!!
>
> Hi main funs,
> 
>  I don't want another war of main style declarations, but ... the void main()
> is very suitable for some platform like a microcontroller, if you put int
> main() in a microcontroller what's the use of the return value!!!, and you'll
> save some space.

Okay, boys and girls, get out your thinking caps.  I've said this once
before and I'm not going to say it again.  :)

The return value of main() is an integer which forms the exit code of the
program being run.  This exit code can be used in several ways, NONE of
which are controlled by the original writer of the program.

1) It can be ignored.

2) It can be tested for by a shell script which uses the exit code to process
errors, run subsidiary programs, or make calculations.

3) If another program spawned the program in question, the exit code forms the
return value of the system()/spawn*() call which invoked it.

4) Possibly other ways that I don't know about.

Now, in all cases but the former, if main() is declared void OR is not given a
return value via return or exit(), the resulting behavior is UNDEFINED.  Why?
Because all functions with int-sized return values return such values in the
(e)ax register.  Any program which examines the return value will look in (e)ax
to find it.  If a return/exit() is not performed, then that value is whatever
happened to be in (e)ax when the function in question terminated.  This is BAD
behavior.  Consider the following example batch file:

TEST.BAT
--------
@echo off
myprog foo.txt bar.txt baz.txt mystuff.tmp
if errorlevel 1 goto error

myprog2 mystuff.tmp mystuff.dat
goto end

:error
echo Whoops!  MYPROG didn't work!
goto end

:end
if exist mystuff.tmp del mystuff.tmp
--------

Suppose you had constructed this to do some fancy processing.  If 'myprog'
had some goofy return value because you hadn't bothered to declare main
right, you could end up being unable to use the (perfectly valid) batch
file and would have to do things manually.  Imagine what would happen if
'cc1.exe' were to have its main declared void.

The term "microcontroller" is not familiar to me, but it doesn't really
matter.  Even if a program is somehow compiled as a subprogram of a larger
one, it still exists as a _separate module_ and there is no guarantee that
somebody won't try to put it in a situation where its (undefined) return
code is needed.

In short, there is NO WAY to absolutely guarantee that your program's
return code will NEVER be tested.

I rest my case.

ADDENDA:  (included to (hopefully) forestall flaming by idiots)

1) Yes, I know you can use exit() to force your program to return something.
This is perfectly fine, but it still doesn't account for the case of falling
out of main() naturally.  Even if you do nothing else, you should always put
a 'return 0;' at the end of main, just in case.  Putting an exit(0) as the
last line of your program as a way to get out of this requirement is a feeble
way of avoiding the problem.

2) (e)ax applies only to the 80*86 processor family.  Other processors use
different register names, but the concept is the same.

3) There may be many smart compilers out there that will force main() to
return a zero value if you declare it void.  Nobody should depend on such
a feature, no matter how pervasive, because it is not ANSI standard behavior.
Sooner or later you'll run into a compiler that doesn't force the value and
get nuked.  Those who complain that ANSI should be changed should note that
ANSI is a standard, not a crutch for poor programming style.

-- 
--------------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I    |         fighteer AT cs DOT com           |
| Plan:  To find ANYONE willing to   |    http://www.cs.com/fighteer     |
| play Descent 2 on DWANGO!          |  Tagline: <this space for rent>   |
--------------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019