Strange function use

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Klein

    #16
    Re: Strange function use

    On Thu, 03 Aug 2006 12:17:14 +0200, Christian Christmann
    <plfriko@yahoo. dewrote in comp.lang.c:
    On Thu, 03 Aug 2006 02:51:39 -0700, lovecreatesbeau ty wrote:
    >

    Christian Christmann wrote:
    1 int foo( int f )
    2 {
    3 return f;
    4 }
    5
    6 int main( void )
    7 {
    8 int a, b, foo();
    int a, b, foo( int );
    9 a = 10;
    10 b = foo( a );
    11
    12 return 0;
    13 }
    >
    I don't understand the use of function "foo" in line 8.
    What's it's purpose? It's called without assigning it's
    return value to any variable. Furthermore, I wonder
    return value to any variable. Furthermore, I wonder
    why this function use is allowed at all. According to
    the function prototype "foo" expects an integer argument
    that is in line 8 not given. Compiling the code with
    "gcc -Wall -ansi" does not issue any warning/errors.
    int foo( ); /*int foo( int );*/

    It's not a function use/call, foo() occurs inside a declaration. It's a
    function prototype declaration.
    >
    But it's a declaration that deviates from the declaration
    given in line 1. There's no function "foo" defined without
    any parameters, so why is the declaration from line 8
    accepted by a compiler?
    Part of your confusion is based on a misunderstandin g of this:

    int foo(int);

    ....regardless of the fact that it as at block scope rather than file
    scope.

    The declaration (not prototype) above declares foo to be a function
    that returns an int and accepts an unspecified but fixed number and
    type of arguments.

    It most specifically is NOT a prototype for a function that takes no
    arguments. In another language that stole much of an earlier version
    of C, these two declarations are equivalent:

    int foo();
    int foo(void);

    ....that is not the case in real, genuine, C.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    • Keith Thompson

      #17
      Re: Strange function use

      Jack Klein <jackklein@spam cop.netwrites:
      [...]
      Part of your confusion is based on a misunderstandin g of this:
      >
      int foo(int);
      >
      ...regardless of the fact that it as at block scope rather than file
      scope.
      >
      The declaration (not prototype) above declares foo to be a function
      that returns an int and accepts an unspecified but fixed number and
      type of arguments.
      "int foo(int);" is both a declaration and a prototype.

      The declaration in the original code was "int foo();", which is a
      declaration but not a prototype. (Actually it was "int a, b, foo();",
      which is an odd way of saying the same thing.)
      It most specifically is NOT a prototype for a function that takes no
      arguments. In another language that stole much of an earlier version
      of C, these two declarations are equivalent:
      >
      int foo();
      int foo(void);
      >
      ...that is not the case in real, genuine, C.
      Ok, so I think you *meant* to say "int foo();" rather than "int foo(int);".

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
      San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
      We must do something. This is something. Therefore, we must do this.

      Comment

      • lovecreatesbeauty

        #18
        Re: Strange function use


        Keith Thompson wrote:
        "lovecreatesbea uty" <lovecreatesbea uty@gmail.comwr ites:
        int foo( ); /*int foo( int );*/
        It's not a function use/call, foo() occurs inside a declaration. It's a
        function prototype declaration.
        >
        It's a function declaration, but not a prototype. To be a prototype,
        it would have to declare the types of the parameters.
        >
        With empty parentheses, it specifies that foo takes an fixed but
        unspecified number and type(s) of arguments. (To specify that it
        takes no arguments, you'd declare "int foo(void)".)
        I remember that int f(){}; int f2(); are obsolete.

        Function prototype is a kind of declaration, right? The function
        getchar has no parameters. The code includes <stdio.hbefor e calls it,
        does not it? I think this is giving the function prototype.

        Comment

        • Ben Pfaff

          #19
          Re: Strange function use

          "lovecreatesbea uty" <lovecreatesbea uty@gmail.comwr ites:
          I remember that int f(){}; int f2(); are obsolete.
          No semicolon follows a function definition.
          --
          Here's a tip: null pointers don't have to be *dull* pointers!

          Comment

          • Keith Thompson

            #20
            Re: Strange function use

            "lovecreatesbea uty" <lovecreatesbea uty@gmail.comwr ites:
            Keith Thompson wrote:
            >"lovecreatesbe auty" <lovecreatesbea uty@gmail.comwr ites:
            int foo( ); /*int foo( int );*/
            It's not a function use/call, foo() occurs inside a declaration. It's a
            function prototype declaration.
            >>
            >It's a function declaration, but not a prototype. To be a prototype,
            >it would have to declare the types of the parameters.
            >>
            >With empty parentheses, it specifies that foo takes an fixed but
            >unspecified number and type(s) of arguments. (To specify that it
            >takes no arguments, you'd declare "int foo(void)".)
            >
            I remember that int f(){}; int f2(); are obsolete.
            >
            Function prototype is a kind of declaration, right?
            Right. Specifically, it's "a declaration of a function that declares
            the types of its parameters" (quoting the standard).

            Now that I think about it, there's a small hole in that definition
            for parameterless functions. "int foo(void)" is a prototype, but
            "int foo()" is not, but one could argue that neither one "declares
            the types of its parameters"; rather, one specifies that there are
            no parameters and the other doesn't.
            The function
            getchar has no parameters. The code includes <stdio.hbefor e calls it,
            does not it? I think this is giving the function prototype.
            Correct, the prototype is in <stdio.h>.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • ena8t8si@yahoo.com

              #21
              Re: Strange function use


              Eric Sosman wrote:
              spibou@gmail.co m wrote:
              >
              Eric Sosman wrote:

              The "prototype" is the description of the function's
              >argument list, either in a declaration or a definition (a
              >definition also declares). The prototype part is
              >
              > int bar(double trouble);
              > ^^^^^^^^^^^^^^

              So the return type is not part of the prototype ?
              Why not ?
              >
              "Why not?" Because of 6.9.1/7:
              >
              "The declarator in a function definition specifies the
              name of the function being defined and the identifiers
              of its parametsrs. [Note omission of return type.] If
              the declarator includes a parameter type list, [...]
              such a declarator also serves as a function prototype
              for later calls to the same function [...]"
              Despite the return type not being mentioned, a function's return
              type can't be completely divorced from its declarator:

              int fee(int), *fie(int), **foe(int), ***fum(int);

              To me it makes just as much sense to say a declarator does
              specify its return type, at least implicitly by virtue of
              what type specifiers it comes after.
              Since the declarator "serves as a function prototype" even though
              it does not include the function's return type, a prototype does
              not include the return type.
              >
              ... and yet, there seem to be contradictions. 6.2.1/2:
              >
              "[...] (A _function prototype_ is a declaration of
              a function that declares the types of its parameters.)"
              >
              A function declaraTION (not declaraTOR) *does* include the return
              type, so this passage says you're right and I'm wrong. Hard to
              say which paragraph wins out.
              >
              But then there's 6.7.5.3/1:
              >
              "A function declarator shall not specify a return type
              that is a function type or an array type."
              >
              ... implying that a declaraTOR does incorporate the return type
              (or the constraint would be vacuous). I think this must be a
              typo for declaraTION, because the grammar of 6.7/1 shows that
              the return type is part of the declaration-specifiers, separate
              from the init-declarator-list that eventually contains the
              declarator.
              Actually only part of the return type is in the declaration
              specifiers:

              int foo()[4]; /* yes this isn't legal */

              The int-ness is in the declaration specifiers; the
              array-sub-4-ness is in the declarator.
              Hence, according to the grammar the function's
              return type is not part of the declaraTOR, but is part of the
              declaraTION.
              Here's my guess for the source of the confusion. A
              declarator is too small, a declaration is too big.
              In the declaration

              int blah(int z(void));
              -----------
              ---------------------

              there are supposed to be two prototypes (underlined).
              The problem is what to say about z: if that's really
              the prototype, it's bigger than a declarator, but not
              as big as a whole declaration.

              The easiest way to make sense of it all is to read the
              word "declaratio n" in 6.2.1/2 as being used informally
              rather than in its technically defined sense.

              Comment

              Working...