is this definition old-style or new-style

Discussion in 'C Programming' started by G Fernandes, Feb 27, 2005.

  1. G Fernandes

    G Fernandes Guest

    Is the following an old-style(K&R) function definition or a new
    style(C89)?

    int (length, factor, multiple)
    {
    /* ... code ... */

    return length;
    }


    It seems like it fits the description of either type. How does the
    compiler know the difference?

    Thanks
     
    G Fernandes, Feb 27, 2005
    #1
    1. Advertising

  2. "G Fernandes" <> wrote in message
    news:...
    > Is the following an old-style(K&R) function definition or a new
    > style(C89)?
    >
    > int (length, factor, multiple)
    > {
    > /* ... code ... */
    >
    > return length;
    > }
    >
    >
    > It seems like it fits the description of either type. How does the
    > compiler know the difference?
    >
    > Thanks
    >

    Niether, int is a keyword, and there is not function declaration going on
    here.

    int PROCEDURE_NAME(int length, int factor, int multiple)
    {
    /* ... code ... */
    return length;
    }

    is a func def, and is old and new.
     
    DHOLLINGSWORTH2, Feb 27, 2005
    #2
    1. Advertising

  3. G Fernandes

    G Fernandes Guest

    DHOLLINGSWORTH2 wrote:
    > "G Fernandes" <> wrote in message
    > news:...
    > > Is the following an old-style(K&R) function definition or a new
    > > style(C89)?
    > >
    > > int (length, factor, multiple)
    > > {
    > > /* ... code ... */
    > >
    > > return length;
    > > }
    > >
    > >
    > > It seems like it fits the description of either type. How does the
    > > compiler know the difference?
    > >
    > > Thanks
    > >

    > Niether, int is a keyword, and there is not function declaration

    going on
    > here.
    >


    What do you mean? Of course int is a keyword.

    >
    > int PROCEDURE_NAME(int length, int factor, int multiple)
    > {
    > /* ... code ... */
    > return length;
    > }
    >
    > is a func def, and is old and new.


    No, the defintion you gave is not an old style(K&R style) definition.
    It's a C89/C99 style definition.


    This below definition fits the description of a C89 and K&R style
    definition (not valid in C99).

    foo(length, factor, multiple) {/* body */}

    I was just wondering how the compiler treats this (new or old way).
     
    G Fernandes, Feb 27, 2005
    #3
  4. In article <>,
    G Fernandes <> wrote:
    :This below definition fits the description of a C89 and K&R style
    :definition (not valid in C99).

    :foo(length, factor, multiple) {/* body */}

    Wouldn't old style be:

    foo(length, factor, multiple) length, factor, multiple; { /* body */ }

    Old style expects variable declarations, and the way to implicitly
    declare a variable as int is to mention it's name but not a type.

    Mind you, it's been long enough since I did old style...
    --
    Pity the poor electron, floating around minding its own business for
    billions of years; and then suddenly Bam!! -- annihilated just so
    you could read this posting.
     
    Walter Roberson, Feb 27, 2005
    #4
  5. G Fernandes

    Chris Torek Guest

    >In article <>,
    >G Fernandes <> wrote:
    >:This below definition fits the description of a C89 and K&R style
    >:definition (not valid in C99).
    >:foo(length, factor, multiple) {/* body */}


    In article <cvt79p$958$>,
    Walter Roberson <-cnrc.gc.ca> wrote:
    >Wouldn't old style be:
    >
    >foo(length, factor, multiple) length, factor, multiple; { /* body */ }


    That would be allowed (with implicit int), but not required.

    In K&R C, one could write:

    main(argc, argv)
    char **argv;
    {
    /* the usual code for main */
    return 0;
    }

    for instance, to let both main() and argc default to "int", and
    give argv the correct type.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Feb 27, 2005
    #5
  6. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > G Fernandes <> wrote:
    > :This below definition fits the description of a C89 and K&R style
    > :definition (not valid in C99).
    >
    > :foo(length, factor, multiple) {/* body */}
    >
    > Wouldn't old style be:
    >
    > foo(length, factor, multiple) length, factor, multiple; { /* body */ }
    >
    > Old style expects variable declarations, and the way to implicitly
    > declare a variable as int is to mention it's name but not a type.


    No. It's:

    foo(length, factor, multiple)
    {
    /* body */
    }

    or, with explicit types:

    int foo(length, factor, multiple)
    int length;
    int factor;
    int multiple;
    {
    /* body */
    }

    Any C89/C90 compiler will accept old-style function definitions; you
    could try compiling your sample code to see that it's invalid.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Feb 27, 2005
    #6
  7. G Fernandes

    Joe Wright Guest

    G Fernandes wrote:
    > DHOLLINGSWORTH2 wrote:
    >
    >>"G Fernandes" <> wrote in message
    >>news:...
    >>
    >>>Is the following an old-style(K&R) function definition or a new
    >>>style(C89)?
    >>>
    >>>int (length, factor, multiple)
    >>>{
    >>> /* ... code ... */
    >>>
    >>> return length;
    >>>}
    >>>
    >>>
    >>>It seems like it fits the description of either type. How does the
    >>>compiler know the difference?
    >>>
    >>>Thanks
    >>>

    >>
    >>Niether, int is a keyword, and there is not function declaration

    >
    > going on
    >
    >>here.
    >>

    >
    >
    > What do you mean? Of course int is a keyword.
    >
    >
    >>int PROCEDURE_NAME(int length, int factor, int multiple)
    >> {
    >> /* ... code ... */
    >> return length;
    >> }
    >>
    >>is a func def, and is old and new.

    >
    >
    > No, the defintion you gave is not an old style(K&R style) definition.
    > It's a C89/C99 style definition.
    >
    >
    > This below definition fits the description of a C89 and K&R style
    > definition (not valid in C99).
    >
    > foo(length, factor, multiple) {/* body */}
    >
    > I was just wondering how the compiler treats this (new or old way).
    >


    G - Your first example in not a function definition at all. It doesn't
    have a function name. Old-Style definitions look like ..

    int foo(len, fac, mul) int len; float fac; float mul; { /* body */ }

    ... while C89 and onward looks like ..

    int foo(int len, float fac, float mul) { /* body */ }

    This latter allows the function to be declared before use or prototyped
    so that the compiler knows exactly how to call it and with which
    argument value conversions. Consider ..

    int foo(int, float, float);

    ... is a prototype. The compiler, having read this prototype, when it
    finds the statement ..

    foo(1.0, 2, 3);

    ... will convert the arguments to int, float and float before calling foo().

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Feb 27, 2005
    #7
  8. On Sun, 27 Feb 2005 09:58:27 -0800, G Fernandes wrote:

    > Is the following an old-style(K&R) function definition or a new
    > style(C89)?
    >
    > int (length, factor, multiple)
    > {
    > /* ... code ... */
    >
    > return length;
    > }
    >
    >
    > It seems like it fits the description of either type. How does the
    > compiler know the difference?


    New style "prototype" declarations and definitions *always* have type
    information between the ()'s. The example above doesn't so is a K&R style
    definition. The prototype form would be

    int (int length, int factor, int multiple)
    {

    Lawrence
     
    Lawrence Kirby, Feb 28, 2005
    #8
  9. Walter Roberson wrote:

    > In article <>,
    > G Fernandes <> wrote:
    > :This below definition fits the description of a C89 and K&R style
    > :definition (not valid in C99).
    >
    > :foo(length, factor, multiple) {/* body */}
    >
    > Wouldn't old style be:
    >
    > foo(length, factor, multiple) length, factor, multiple; { /* body */ }
    > ...


    C89/90 allows selective declaration of function parameters in
    declaration list. You can declare all of them

    int foo(length, factor, multiple)
    int length, factor, multiple;
    { /* body */ }

    you can declare none

    int foo(length, factor, multiple)
    { /* body */ }

    you can declare only some of them

    int foo(length, factor, multiple)
    int factor;
    { /* body */ }

    All these variants are fine in C89/90.

    However, in C99 "old-style" function definitions are required to declare
    all named parameters in declaration list, meaning that in C99 only the
    first variant is valid.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 1, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dlo_olb
    Replies:
    2
    Views:
    2,311
    Fredrik Lundh
    Jun 25, 2003
  2. Kza
    Replies:
    4
    Views:
    422
    Andrew Koenig
    Mar 3, 2006
  3. John Henry
    Replies:
    24
    Views:
    1,020
    alex23
    May 30, 2008
  4. john
    Replies:
    8
    Views:
    372
    Nick Keighley
    Jun 22, 2010
  5. Jayden
    Replies:
    9
    Views:
    299
    alex23
    Sep 27, 2012
Loading...

Share This Page