Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Dec 2001 11:17:34 +0100 (CET)
From:      Harti Brandt <brandt@fokus.gmd.de>
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:  <20011218104454.U41838-100000@beagle.fokus.gmd.de>
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:

ET>On Tue, Dec 18, 2001 at 10:48:56AM +0200, Mark Murray wrote:

[SNIP]

ET>>
ET>> AFAIK, this is perfectly legal C:
ET>
ET>Not quite.
ET>
ET>>
ET>> /* begin */
ET>> void printf(char *, ...)
ET>
ET>Since you did not #include <stdio.h> I guess you get away with this.
ET>(Most library functions are not reserved unless the relevant header is
ET>included.)

Not true, section 7.1.3 of the standard says, that all names with external
linkage in the subsection following 7.1.3 are reserved. This also includes
printf().

ET>
ET>>
ET>> void main(void)
ET>
ET>main should always return an int.

This is not entirely true. In the text you provide below from the C99
draft you stripped the sentence the allows an hosted environment to define
whatever main() function it decides to define. Appendix K.3.2 lists the
implementation-defined behaviour for the environment:

- An alternative manner in which the main function may be defined.

But, this is just to be theoretically correct :-). Our environment defines
main() as returning int, so correct programs should declare and define
main() main as returning int. But, given that the compiler has no
knowledge about main() (the text below states, that the compiler does NOT
declare main()), it cannot give you an error.

ET>
ET>> {
ET>> 	printf("Hello world");
ET>> }
ET>> /* end */
ET>
ET>You haven't defined the 'printf' anywhere.
ET>If you want to use the standard printf function you need to include
ET><stdio.h>

Clause 7.1.4 of my C draft states that you can use any function, if it can
be declared without types that are defined in the associate headers,
without including the header. So calling printf() without stdio.h is
legal.

ET>>
ET>> And it should compile warning-free and run without error. Agreed
ET>
ET>No it should not.
ET>
ET>> that the style sucks, but it is _legal_ - and any compiler's prior
ET>> assumed knowledge about main is plain wrong - it is a linker thing
ET>> to use ``main'' as an entry point, and nobody else's damn business
ET>> what it is after that! (argc and argv are likewise conventions that
ET>> are less useful in an embedded environment with no shell (ya, ya I
ET>> know about execv :)).
ET>>
ET>> Now if anyone can show official standards showing me that I'm
ET>> wrong here, I'll shut up and back off. :-)
ET>
ET>The C standard does not make a distinction between compiler and linker,
ET>nor should it.
ET>A compiler is allowed to have a lot of knowledge about the standard
ET>functions.

But it makes a distinction between the compiler and the environment. The
knowledge of what main() looks like is part of the environment, not the
compiler.

ET>For a hosted environment it has the following to say about main:
ET>(From a draft for the C99 standard)
ET>
ET>       5.1.2.2.1  Program startup
ET>
ET>       [#1]  The  function called at program startup is named main.
ET>       The implementation declares no prototype for this  function.
ET>       It  shall  be  defined with a return type of int and with no
ET>       parameters:
ET>
ET>               int main(void) { /* ... */ }
ET>
ET>       or with two parameters (referred to here as argc  and  argv,
ET>       though  any  names  may  be  used,  as they are local to the
ET>       function in which they are declared):
ET>
ET>               int main(int argc, char *argv[]) { /* ... */ }
ET>
ET>
ET>For a freestanding environment (which can be useful for embedded stuff)
ET>you don't have any requirements on main().  OTOH about 95% of the
ET>standard library is not guaranteed to be available either.
ET>The only header files which may be included in a strictly program for a
ET>freestanding implementation are
ET><float.h>,   <iso646.h>,   <limits.h>, <stdarg.h>,  <stdbool.h>,
ET><stddef.h>,  and  <stdint.h>
-- 
harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private
              brandt@fokus.fhg.de


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?20011218104454.U41838-100000>