function prototypes

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jan 18, 2006.

  1. Guest

    Can a function have two different prototypes ? If not , then how can
    main() have
    two different prototypes ?
    int main(void)
    and
    int main argc(int argc, char *argv[])
    I mean to say, if I declare main in either of the above mentioned ways
    my compiler
    does not give any warning. How can it accept two different prototypes
    ?

    Thanx for any help in advance ..
     
    , Jan 18, 2006
    #1
    1. Advertising

  2. Vladimir Oka Guest

    wrote:
    > Can a function have two different prototypes ? If not , then how can
    > main() have
    > two different prototypes ?
    > int main(void)
    > and
    > int main argc(int argc, char *argv[])
    > I mean to say, if I declare main in either of the above mentioned ways
    > my compiler
    > does not give any warning. How can it accept two different prototypes
    > ?
    >
    > Thanx for any help in advance ..


    A C function cannot have two (or more) different prototypes in one
    program.

    A function with the same name can have different prototypes in all the
    different programs you care to use it in, and that includes main() as
    well.

    For example, these are legal, as they have no relation whatsoever:

    ---- BEG app1.c ----
    int func(void);

    int main(void)
    {
    return func();
    }

    int func(void)
    {
    return 0;
    }
    ---- END app1.c ----

    ---- BEG app2.c ----
    double func(double x);

    int main(int argc, char *argv[])
    {
    double y;

    y = func(2.0L);

    return argc;
    }

    double func(double x)
    {
    return -x;
    }
    ---- END app2.c ----

    Whereas, this is not:

    ---- BEG app3.c ----
    int func(void);
    double func(double x);

    int main(void)
    {
    return func();
    }

    int func(void)
    {
    return 0;
    }

    double func(double x)
    {
    return -x;
    }
    ---- END app3.c ----

    Hope this helps.

    Cheers

    Vladimir
     
    Vladimir Oka, Jan 18, 2006
    #2
    1. Advertising

  3. Flash Gordon Guest

    wrote:
    > Can a function have two different prototypes ?


    No, apart from main.

    > If not , then how can
    > main() have
    > two different prototypes ?
    > int main(void)
    > and
    > int main argc(int argc, char *argv[])
    > I mean to say, if I declare main in either of the above mentioned ways
    > my compiler
    > does not give any warning. How can it accept two different prototypes
    > ?


    It's magic. Or, to be more precise, main is the one and only exception
    because the standard explicitly states that you can use those two forms
    for main.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 18, 2006
    #3
  4. S.Tobias Guest

    Flash Gordon <> wrote:
    > wrote:
    >> Can a function have two different prototypes ?

    >
    > No, apart from main.
    >

    This may or may not be correct depending on how you understand
    "a function having a prototype" (is it "function _defined_ with
    a prototype" - then yes, there can only be one function definition).
    You can _declare_ a function multiple times with different prototypes,
    or without (provided the declared function types are compatible);
    for an example, see n869.txt 6.2.7 ("Compatible type and composite
    type") #5.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Jan 18, 2006
    #4
  5. Flash Gordon <> writes:
    > wrote:
    > > Can a function have two different prototypes ?

    > No, apart from main.
    > > If not, then how can main() have two different prototypes ?

    > It's magic. Or, to be more precise, main is the one and only exception
    > because the standard explicitly states that you can use those two
    > forms for main.


    The Standard also explicitly states that the implementation must not
    provide a prototype for main(), so the first part of your answer is
    incorrect. If there exists a prototype for main(), other than the one
    that results from its definition, it is provided by the application,
    and must match the definition.

    DES
    --
    Dag-Erling Smørgrav -
     
    =?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=, Jan 18, 2006
    #5
  6. Flash Gordon Guest

    S.Tobias wrote:
    > Flash Gordon <> wrote:
    >> wrote:
    >>> Can a function have two different prototypes ?

    >> No, apart from main.
    >>

    > This may or may not be correct depending on how you understand
    > "a function having a prototype" (is it "function _defined_ with
    > a prototype" - then yes, there can only be one function definition).
    > You can _declare_ a function multiple times with different prototypes,
    > or without (provided the declared function types are compatible);
    > for an example, see n869.txt 6.2.7 ("Compatible type and composite
    > type") #5.


    I took different prototypes to mean incompatible prototypes, especially
    as the example given was "int main(void)" and "int main(int argc, char
    *argv[])" which are clearly incompatible.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 18, 2006
    #6
  7. Flash Gordon <> writes:
    > wrote:
    >> Can a function have two different prototypes ?

    >
    > No, apart from main.
    >
    > > If not , then how can
    >> main() have
    >> two different prototypes ?
    >> int main(void)
    >> and
    >> int main argc(int argc, char *argv[])
    >> I mean to say, if I declare main in either of the above mentioned ways
    >> my compiler
    >> does not give any warning. How can it accept two different prototypes
    >> ?

    >
    > It's magic. Or, to be more precise, main is the one and only exception
    > because the standard explicitly states that you can use those two
    > forms for main.


    main() is the one and only exception, but not in that way.

    Any function other than main() can be defined any way you like. You
    can declare a function foo() with any number and type of arguments you
    like; likewise for bar() or for just about anything else. main() is
    the only function on which the implementation places restrictions: you
    can only use one of the two standard forms, or some other
    implementation-defined form. main() is unique because it's called by
    the environment; other functions are called by your own program.

    It would have been more consistent for the implementation to provide a
    single prototype for main() and require you to provide a definition
    consistent with that prototype. The existing special-case rules are
    there for historical reasons.

    --
    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, Jan 18, 2006
    #7
  8. Jordan Abel Guest

    On 2006-01-18, Flash Gordon <> wrote:
    > S.Tobias wrote:
    >> Flash Gordon <> wrote:
    >>> wrote:
    >>>> Can a function have two different prototypes ?
    >>> No, apart from main.
    >>>

    >> This may or may not be correct depending on how you understand
    >> "a function having a prototype" (is it "function _defined_ with
    >> a prototype" - then yes, there can only be one function definition).
    >> You can _declare_ a function multiple times with different prototypes,
    >> or without (provided the declared function types are compatible);
    >> for an example, see n869.txt 6.2.7 ("Compatible type and composite
    >> type") #5.

    >
    > I took different prototypes to mean incompatible prototypes, especially
    > as the example given was "int main(void)" and "int main(int argc, char
    > *argv[])" which are clearly incompatible.


    well, in that case you can have int foo(double), void foo(int *,
    double), double foo(FILE *), or any other imaginable combination. you
    certainly can't have _both_ prototypes for main in one program.
     
    Jordan Abel, Jan 19, 2006
    #8
    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. Denis Remezov

    order of const in function prototypes

    Denis Remezov, Jun 30, 2004, in forum: C++
    Replies:
    7
    Views:
    438
    John Harrison
    Jun 30, 2004
  2. Bo Jacobsen
    Replies:
    6
    Views:
    516
    David M. Cook
    Mar 7, 2004
  3. Chris Dollin

    Re: Can function prototypes appear anywhere?

    Chris Dollin, Jan 19, 2005, in forum: C Programming
    Replies:
    3
    Views:
    361
    Michael Wojcik
    Jan 19, 2005
  4. Lawrence Kirby

    Re: Can function prototypes appear anywhere?

    Lawrence Kirby, Jan 20, 2005, in forum: C Programming
    Replies:
    2
    Views:
    343
    Lawrence Kirby
    Jan 24, 2005
  5. Syren Baran

    function prototypes and function addresses

    Syren Baran, Jan 9, 2008, in forum: C Programming
    Replies:
    6
    Views:
    314
Loading...

Share This Page