What's the difference between function declaration and function prototype?

Discussion in 'C Programming' started by Hill Pang, Aug 7, 2012.

  1. Hill Pang

    Hill Pang Guest

    I can find some answer from web, but I can't find their definitions in the C99 standard so far.

    Which page is it in?

    Thanks in advance.
     
    Hill Pang, Aug 7, 2012
    #1
    1. Advertising

  2. Hill Pang <> writes:
    > I can find some answer from web, but I can't find their definitions in
    > the C99 standard so far.
    >
    > Which page is it in?


    A function prototype is a *kind* of function declaration, one that
    specifies the types of the parameters. A non-prototype declaration
    has empty parentheses; it's also known as an old-style or K&R-style
    declaration, and it's obsolescent.

    The term "prototype" is defined in C99 and C11 6.2.1p2:

    A *function prototype* is a declaration of a function that declares
    the types of its parameters.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Aug 7, 2012
    #2
    1. Advertising

  3. Hill Pang

    Varun Tewari Guest

    There a little difference, but in modern context, they are same.
     
    Varun Tewari, Aug 7, 2012
    #3
  4. Hill Pang

    Eric Sosman Guest

    Re: What's the difference between function declaration and functionprototype?

    On 8/7/2012 1:30 PM, Varun Tewari wrote:
    > There a little difference, but in modern context, they are same.


    That seems misleading. Keith Thompson quoted the actual
    definition: "A function prototype is a declaration of a function
    that declares the types of its parameters." The very definition
    suggests the existence of function declarations that omit the
    parameter types, and indeed if you look elsewhere in the Standard
    you will find the rules for declaring functions this way.

    By "in modern context," I imagine you mean something like
    "always use prototypes, because a compiler that can't handle them
    cannot possibly be `modern'." That's good advice: There is almost
    never a reason to omit the parameter types, and I'd venture that
    even when a reason exists it's probably a weak one. The Standard
    describes non-prototype function declarations as an "obsolecent
    feature" of the language, and even if it never gets all the way to
    "obsolete" the message is clear: Don't Do That.

    Still, when language feature F comes in forms F1 and F2, with
    F1 discouraged and F2 strongly recommended, I think someone who
    asks "What's the difference between F and F2" deserves more of an
    answer than "Never mind."

    --
    Eric Sosman
    d
     
    Eric Sosman, Aug 7, 2012
    #4
  5. Hill Pang

    Guest

    prototype and declaration both are same
     
    , Aug 16, 2012
    #5
  6. writes:
    > prototype and declaration both are same


    That's incorrect. As I wrote in my response last week:

    The term "prototype" is defined in C99 and C11 6.2.1p2:

    A *function prototype* is a declaration of a function that declares
    the types of its parameters.

    --
    Keith Thompson (The_Other_Keith) <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"
     
    Keith Thompson, Aug 16, 2012
    #6
  7. Hill Pang

    James Kuyper Guest

    Re: What's the difference between function declaration and functionprototype?

    On 08/16/2012 05:37 AM, wrote:
    > prototype and declaration both are same


    Are you claiming that "int func();" is a function prototype? Or are you
    claiming that it's not a function declaration?
    --
    James Kuyper
     
    James Kuyper, Aug 16, 2012
    #7
  8. On Aug 7, 6:55 pm, Eric Sosman <> wrote:
    > On 8/7/2012 1:30 PM, Varun Tewari wrote:


    > > There a little difference, but in modern context, they are same.


    <snip>

    >      By "in modern context," I imagine you mean something like
    > "always use prototypes, because a compiler that can't handle them
    > cannot possibly be `modern'."  That's good advice: There is almost
    > never a reason to omit the parameter types, and I'd venture that
    > even when a reason exists it's probably a weak one.


    maintaining K&R style code. Yes it's still about. It's only a couple
    of years since I modified a K&R program.

    >  The Standard
    > describes non-prototype function declarations as an "obsolecent
    > feature" of the language, and even if it never gets all the way to
    > "obsolete" the message is clear: Don't Do That.


    <snip>
     
    Nick Keighley, Aug 16, 2012
    #8
  9. Hill Pang

    Phil Carmody Guest

    Kenneth Brody <> writes:
    > On 8/16/2012 8:18 AM, Nick Keighley wrote:
    > > On Aug 7, 6:55 pm, Eric Sosman <> wrote:
    > >> On 8/7/2012 1:30 PM, Varun Tewari wrote:

    > >
    > >>> There a little difference, but in modern context, they are same.

    > >
    > > <snip>
    > >
    > >> By "in modern context," I imagine you mean something like
    > >> "always use prototypes, because a compiler that can't handle them
    > >> cannot possibly be `modern'." That's good advice: There is almost
    > >> never a reason to omit the parameter types, and I'd venture that
    > >> even when a reason exists it's probably a weak one.

    > >
    > > maintaining K&R style code. Yes it's still about. It's only a couple
    > > of years since I modified a K&R program.

    > [...]
    >
    > Until recently, the code I maintain had to be able to compile on
    > HP-UX. The C compiler that came with the system would recognize
    > prototypes, and tell you that you needed to buy their other C compiler
    > if you needed prototype support.
    >
    > As I recall, there is now a gcc for HP-UX, eliminating that hurdle.


    I was using gcc in HP-UX back in the 90s. I know I had access to a range
    between 2.5 and 2.95, which covers most of the decade.

    Phil
    --
    > I'd argue that there is much evidence for the existence of a God.

    Pics or it didn't happen.
    -- Tom (/. uid 822)
     
    Phil Carmody, Sep 5, 2012
    #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. jakk
    Replies:
    4
    Views:
    12,233
  2. Ravishankar S

    function prototype vs function declaration

    Ravishankar S, Dec 18, 2007, in forum: C Programming
    Replies:
    44
    Views:
    1,552
    Flash Gordon
    Dec 23, 2007
  3. June Lee
    Replies:
    2
    Views:
    809
    Jim Cobban
    Apr 13, 2008
  4. Replies:
    9
    Views:
    199
    Thomas 'PointedEars' Lahn
    May 26, 2006
  5. Daz
    Replies:
    5
    Views:
    112
Loading...

Share This Page