Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1517

Re: Is this a prototype or not

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Re: Is this a prototype or not
Date 2012-08-17 13:05 -0700
Organization None to speak of
Message-ID <lnobm9nv2c.fsf@nuthaus.mib.org> (permalink)
References <4F8343A5.6060402@spamsink.net> <8c4a471e-6f90-4b7f-b979-e538dba7c907@googlegroups.com>

Show all headers | View raw


Vlad from Moscow <vlad.moscow@mail.ru> writes:
[...]
> A parameter type list may not be empty. It shall contain at least a
> single parameter of type void.  A function prototype is a function
> declaration (without definition) that has a parameter type list. A
> function prototype defines a prototype scope. A function definition
> with a parameter type list also serves as a function prototype but it
> does not define a prototype scope.

I agree with what you're saying, but it's not a quotation from
the standard.  Can you support it by citing specific wording in
the standard?

Again, N1370 6.7.6.3p14 says:

    An identifier list declares only the identifiers of the
    parameters of the function. An empty list in a function
    declarator that is part of a definition of that function
    specifies that the function has no parameters. The empty list
    in a function declarator that is not part of a definition of
    that function specifies that no information about the number
    or types of the parameters is supplied.

I'd say that a definition with empty parentheses does *not* provide
a prototype, but it could be argued either way.

    void func0() { /* ...*/ }

specifies that func0 has no parameters.  Similarly, this:

    void func1(n) int n; { /* ... */ }

specifies that func1 has one parameter, but it clearly doesn't provide a
prototype.  The specification that func0 has no parameters applies only
to the function definition, specifically to the existence of the local
variables that are initialized from parameter values on each call.

The whole point is to retain compatibility with pre-ANSI C code, in
which prototypes were not available.  In pre-ANSI C, this:

    void func0() { /* ... */ }

clearly did not provide a prototype, and a call such as func0(42) would
have undefined behavior (to the extent that the concept existed), but
probably would not trigger a diagnostic.

C89 added prototypes to the language as a new feature.  I don't think it
intended to reinterpret existing pre-ANSI programs that didn't have
prototypes, saying that they now have prototypes.

On the other hand, I don't think that a ruling that
    void func0() { /* ... */ }
*does* provide a prototype would cause much harm.  I can't think of
any non-buggy code that would be broken.  And of course compilers
are already free to warn about argument mismatches on calls
to non-prototyped functions, if the information is available.
(gcc doesn't do this; perhaps the developers didn't want to spend
much time supporting non-prototyped functions.)

This contrived program:

void func0() { }

int main(void) {
    if (0) func0(42);
    return 0;
}

is a constraint violation if the definition of func0 provides a
prototype, but is strictly conforming if it doesn't.  I believe
it's strictly conforming (but poor style).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.std.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is this a prototype or not jacob navia <jacob@spamsink.net> - 2012-04-09 22:16 +0200
  Re: Is this a prototype or not Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-04-09 23:13 +0100
    Re: Is this a prototype or not Tim Rentsch <txr@alumni.caltech.edu> - 2012-04-09 18:16 -0700
      Re: Is this a prototype or not Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-04-10 02:30 +0100
  Re: Is this a prototype or not Tim Rentsch <txr@alumni.caltech.edu> - 2012-04-09 18:07 -0700
    Re: Is this a prototype or not jacob navia <jacob@spamsink.net> - 2012-04-10 21:11 +0200
      Re: Is this a prototype or not Philip Lantz <prl@canterey.us> - 2012-04-13 00:36 -0700
  Re: Is this a prototype or not Harald van Dijk <haraldvdijk@gmail.com> - 2012-04-10 13:57 -0700
    Re: Is this a prototype or not Nick Bowler <nbowler@draconx.ca> - 2012-08-15 15:56 +0000
      Re: Is this a prototype or not Jens Gustedt <jens.gustedt@loria.fr> - 2012-08-15 19:07 +0200
        Re: Is this a prototype or not Nick Bowler <nbowler@draconx.ca> - 2012-08-23 19:59 +0000
  Re: Is this a prototype or not Philip Lantz <prl@canterey.us> - 2012-04-13 00:25 -0700
  Re: Is this a prototype or not Vlad from Moscow <vlad.moscow@mail.ru> - 2012-08-17 02:49 -0700
    Re: Is this a prototype or not Vlad from Moscow <vlad.moscow@mail.ru> - 2012-08-17 03:14 -0700
    Re: Is this a prototype or not Keith Thompson <kst-u@mib.org> - 2012-08-17 13:05 -0700
      Re: Is this a prototype or not Vlad from Moscow <vlad.moscow@mail.ru> - 2012-08-18 05:27 -0700

csiph-web