main() linkage

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Nov 29 05:24:59 AEST 1989


In article <1122 at cirrusl.UUCP> dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>What is erroneous programming practice?
>One overly simple definition is "programming practice that violates the
>ANSI C standard".  Which is not a very useful definition when
>considering code that was written before the ANSI C standard came into
>being.

That's probably why nobody would propose such a "definition".
However, note that code that unnecessarily depends on details
of a C implementation is inherently less portable than it could
have been.

>I have seen the usual use of printf to be considered an error waiting
>to be fixed.  (I have even seen Ritchie as being quoted as saying
>something like this.)  But if K&R defined the C programming language in
>a certain way, and that definition required printf to be used in a
>certain way, and if millions of lines of existing *correctly working
>code* uses printf, thow can that usage possibly be erroneous?

It's not erroneous, just dependent on a certain practical
implementation constraint (namely, that the abuse of function
linkage exemplified by printf() be somehow made to work) that
X3J11 decided to remove.  The C language never officially
supported functions like printf(), although a reasonable C
implementor would generally take it into account when designing
his function linkage technique, because the customers expected
it to work.  A generally better solution is now officially part
of Standard C.

>On the other hand, existing code can stop working through no fault of
>its own, if compilers are changed to no longer compile it correctly.

That's between you and your compiler vendor.  It has always been
the case that depending on implementation quirks could mean that
your code could stop working with a new release of the compiler.

>So was it portable to declare main() to return void?  As always,
>portability depends on your universe.  Within the universe of C
>implementations that supported the language as defined by K&R, which
>was roughly equivalent to the universe of existing C implementations,
>it was always portable to declare main() to return anything you wanted,
>provided you always existed by calling exit.  *That has not changed*.

Wrong, wrong, wrong.  Misdeclaring function return types has ALWAYS
been a violation of the C language specification, well before X3J11.
You happen to have been able to get away with it on the limited
range of implementations you have had access to.  That does not mean
that your violations of the language spec have been "portable",
merely that you hadn't yet been bit by them.

>What has changed is universe of existing C implementations, and it has
>changed because the meaning of "C" has changed.

That's not the problem..



More information about the Comp.lang.c mailing list