cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/02/19:30:31

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: A nice trap! (2)
Date: Tue, 02 Jun 1998 19:21:29 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 82
Message-ID: <357488F9.6054@cs.com>
References: <35715AA9 DOT 4222FD24 AT net4you DOT co DOT at> <35718841 DOT 1129 AT cs DOT com> <slrn6n7qds DOT 46 DOT loki AT anima DOT nectar DOT com DOT au>
NNTP-Posting-Host: ppp134.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

loki wrote:
> 
> John M. Aldrich <fighteer AT cs DOT com> wrote:
> 
> >BTW, your test program has some significant flaws.  First, main() must
> >return an integer according to the ANSI standard.
> 
>   I agree, this is something a lot of people don't seem to realise, not that
> I think it matters too much as long as people:
> a) realise it technically isn't ANSI-compliant
> b) don't try to get main() to return something!

The problem is that most people who use don't use an integer return type
for main seem to think that it's standard code because that's the way
they were taught.  I am trying, in my own small way, to create greater
awareness of standards among the programming community.  :-)

In any case, there are three practical reasons for using the correct
return type for main():

1) The return value is used as the program's exit code, and may be used
by any program that invokes yours, such as Make, RHIDE, or one of your
own programs via system().  Traditionally, a return value of zero
indicates success, and any other value indicates an error condition. 
Therefore, any parent programs that evaluate the return code will get
bogus information if you declare main() void.  In particular, if you run
your program through the RHIDE interface, you'll get spurious "Program
returned error X" messages.

2) Some rare compiler architectures pass return values on the stack;
thus if main() is declared void the program will crash at runtime.

3) C++ programs use strong data typing and _require_ main() to return an
integer.  In fact, this requirement is so strong that if you declare
main() any other type, the compiler automatically converts it to int. 
Using the '-Wall' option warns you about this.

And of course, if a function returns something, there ought to be a
return statement somewhere in the code.  Thus, the shortest legal ANSI C
program is as follows:

int main( void )
{
    return 0;
}

I recommend reading the comp.lang.c FAQ
(http://www.eskimo.com/~scs/C-faq/top.html); it's about as close as you
can get to an official online reference.  The main() issue is hashed out
in chapter 11.

> > Second, library
> >functions like printf() must be prototyped by including the appropriate
> >standard header file, in this case stdio.h.
> 
>   I'm not sure about this.  I was under the impression that function
> prototypes are not strictly required by ANSI.  Of course it's better to have
> them so as to have better compile-time checking of function arguments, but I
> didn't think it was strictly compulsory.

I'm not sure on this one either, but I do know that prototyping is
something that absolutely should be done regardless of whether the
standard mandates it.  There's simply no excuse not to.  I won't accept
any program that attempts to demonstrate buggy compiler behavior unless
it #includes all necessary headers and has been compiled with full
warning options on (minimum of '-Wall' and '-O', preferably also '-W'
and '-Werror').

Warnings are not something trivial to be ignored; any programmer that
doesn't take full advantage of a compiler's ability to tell him/her
what's wrong with a program is (IMHO) either ignorant or foolish. 
Ignorance by itself, of course, is not a crime, but there are a
tremendous number of resources around here devoted to removing it as an
obstacle.  :-)

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |     mailto:fighteer AT cs DOT com      |
| Proud owner of what might one   |   http://www.cs.com/fighteer/   |
| day be a spectacular MUD...     |       ICQ UIN#:  7406319        |
| Plan: To make Bill Gates suffer |     HEAT User ID:  Fighteer     |
---------------------------------------------------------------------

- Raw text -


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