Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Dec 2001 02:26:04 +1100 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Erik Trulsson <ertr1013@student.uu.se>
Cc:        Mark Murray <mark@grondar.za>, John Baldwin <jhb@FreeBSD.org>, "David O'Brien" <obrien@FreeBSD.org>, <cvs-all@FreeBSD.org>, <cvs-committers@FreeBSD.org>, Luigi Rizzo <rizzo@aciri.org>, Garance A Drosihn <drosih@rpi.edu>
Subject:   Re: Are prototypes for main() illegal by any standard ? (was Re:
Message-ID:  <20011219020451.T4733-100000@gamplex.bde.org>
In-Reply-To: <20011218093631.GA1096@student.uu.se>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 18 Dec 2001, Erik Trulsson wrote:

> On Tue, Dec 18, 2001 at 10:48:56AM +0200, Mark Murray wrote:
> > AFAIK, this is perfectly legal C:
>
> Not quite.
>
> >
> > /* begin */
> > void printf(char *, ...)
>
> Since you did not #include <stdio.h> I guess you get away with this.
> (Most library functions are not reserved unless the relevant header is
> included.)

Not quite.  Non-inclusion of <stdio.h> inhibits detection of the 2
type mismatches in the prototype for printf().  (Oops, I missed one
of these in a previous reply.)  At least the error in the return type
causes undefined behaviour.

> > void main(void)
>
> main should always return an int.
>
> > {
> > 	printf("Hello world");
> > }
> > /* end */
>
> You haven't defined the 'printf' anywhere.

No, it was (mis)defined above.

> If you want to use the standard printf function you need to include
> <stdio.h>

Most standard functions can be declared explictly provided the declaration
is correct.  This is possible for printf().  OTOH, it is not possible for
fprintf() because FILE is too magic to declare explicitly.  A prototype
for printf() is only required because printf() takes a variable number of
args.

> For a hosted environment it has the following to say about main:
> (From a draft for the C99 standard)
>
>        5.1.2.2.1  Program startup
>
>        [#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[]) { /* ... */ }
>

The final clause of this got trimmed somewhere.  From n869.txt:

       or equivalent;8)  or in  some  other  implementation-defined
       manner.

This says that implementation may permit "void main" and other perversions.
This clause is new in C99.  C90 compilers like gcc-2.95 are only required
to support 2 forms of main() (in the hosted case).

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011219020451.T4733-100000>