function call with arguments which takes no arguments

Discussion in 'C Programming' started by Neo, Jan 19, 2005.

  1. Neo

    Neo Guest

    Why the following code is compilable? The function abc() doesn't take any
    arguments, still i can call the function with arbitraty number of arguments.
    Even compiler doesn't show any warning. What the standard says?

    ----- file1.c ------
    extern unsigned abc();
    int main()
    {
    unsigned *chip_offset;
    abc(&chip_offset, 10);
    /* do something with pointer - which is errornous */
    return 0;
    }

    ----- file2.c -----
    unsigned abc()
    {
    unsigned some_address;
    some_address = 0xFFBA;
    return some_address;
    }


    -Neo
    "If you don't program yourself, life will program you!"
     
    Neo, Jan 19, 2005
    #1
    1. Advertising

  2. Neo

    S.Tobias Guest

    Neo <> wrote:

    > Why the following code is compilable? The function abc() doesn't take any
    > arguments, still i can call the function with arbitraty number of arguments.
    > Even compiler doesn't show any warning. What the standard says?


    UB

    (N869)
    6.5.2.2 Function calls
    [#6] If the expression that denotes the called function has
    a type that does not include a prototype, the integer
    promotions are performed on each argument, and arguments
    that have type float are promoted to double. These are
    called the default argument promotions. If the number of
    arguments does not agree with the number of parameters, the
    behavior is undefined. If the function is defined with a

    > ----- file1.c ------
    > extern unsigned abc();


    Here above you declare abc to be a function with unspecified number
    of parameters (ie. function without a prototype). The compiler
    does not assume anything about the number of arguments (except
    that it is constant). (note: this is unlike in C++.)

    This is the declaration of abc as a function with no parameters:

    extern unsigned abc(void);

    > int main()
    > {
    > unsigned *chip_offset;
    > abc(&chip_offset, 10);
    > /* do something with pointer - which is errornous */
    > return 0;
    > }


    > ----- file2.c -----
    > unsigned abc()
    > {
    > unsigned some_address;
    > some_address = 0xFFBA;
    > return some_address;
    > }



    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Jan 19, 2005
    #2
    1. Advertising

  3. Neo

    aegis Guest

    S.Tobias wrote:
    > Neo <> wrote:
    >
    > > Why the following code is compilable? The function abc() doesn't

    take any
    > > arguments, still i can call the function with arbitraty number of

    arguments.
    > > Even compiler doesn't show any warning. What the standard says?

    >
    > UB
    >
    > (N869)
    > 6.5.2.2 Function calls
    > [#6] If the expression that denotes the called function has
    > a type that does not include a prototype, the integer
    > promotions are performed on each argument, and arguments
    > that have type float are promoted to double. These are
    > called the default argument promotions. If the number of
    > arguments does not agree with the number of parameters, the
    > behavior is undefined. If the function is defined with a
    >


    I don't think this applies here. He has a function declarator
    that is not part of a definition, which signals to the
    compiler that there is no information given as to the number of
    arguments to the function. So the use seems to be perfectly okay
    aside from the fact that it is considered obsolescent.

    > > ----- file1.c ------
    > > extern unsigned abc();

    >
    > Here above you declare abc to be a function with unspecified number
    > of parameters (ie. function without a prototype). The compiler
    > does not assume anything about the number of arguments (except
    > that it is constant). (note: this is unlike in C++.)
    >
    > This is the declaration of abc as a function with no parameters:
    >
    > extern unsigned abc(void);
    >
    > > int main()
    > > {
    > > unsigned *chip_offset;
    > > abc(&chip_offset, 10);
    > > /* do something with pointer - which is errornous */
    > > return 0;
    > > }

    >
    > > ----- file2.c -----
    > > unsigned abc()
    > > {
    > > unsigned some_address;
    > > some_address = 0xFFBA;
    > > return some_address;
    > > }

    >
    >
    > --
    > Stan Tobias
    > mailx `echo LID | sed s/[[:upper:]]//g`

    --
    aegis
     
    aegis, Jan 19, 2005
    #3
  4. "aegis" <> writes:
    > S.Tobias wrote:
    >> Neo <> wrote:
    >>
    >> > Why the following code is compilable? The function abc() doesn't

    > take any
    >> > arguments, still i can call the function with arbitraty number of

    > arguments.
    >> > Even compiler doesn't show any warning. What the standard says?

    >>
    >> UB
    >>
    >> (N869)
    >> 6.5.2.2 Function calls
    >> [#6] If the expression that denotes the called function has
    >> a type that does not include a prototype, the integer
    >> promotions are performed on each argument, and arguments
    >> that have type float are promoted to double. These are
    >> called the default argument promotions. If the number of
    >> arguments does not agree with the number of parameters, the
    >> behavior is undefined. If the function is defined with a
    >>

    >
    > I don't think this applies here. He has a function declarator
    > that is not part of a definition, which signals to the
    > compiler that there is no information given as to the number of
    > arguments to the function. So the use seems to be perfectly okay
    > aside from the fact that it is considered obsolescent.


    The "extern" declaration of abc() says it takes an unspecified number
    of arguments. The *definition* of abc() in file2.c says it takes no
    arguments. The call passes two arguments. Undefined behavior.

    --
    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 19, 2005
    #4
  5. Neo

    Mike Wahler Guest

    "Neo" <> wrote in message
    news:...
    > Why the following code is compilable? The function abc() doesn't take any
    > arguments, still i can call the function with arbitraty number of

    arguments.
    > Even compiler doesn't show any warning. What the standard says?
    >
    > ----- file1.c ------
    > extern unsigned abc();


    This prototype does not indicate 'no arguments'.
    It indicates 'arguments not specified'. It's
    also a fragile way to do things.

    -Mike
     
    Mike Wahler, Jan 19, 2005
    #5
  6. Neo

    Mike Wahler Guest

    "Peter Nilsson" <> wrote in message
    news:...
    > Mike Wahler wrote:
    > > "Neo" <> wrote...
    > > > Why the following code is compilable? The function abc() doesn't
    > > > take any arguments, still i can call the function with arbitraty
    > > > number of arguments.
    > > > Even compiler doesn't show any warning. What the standard says?
    > > >
    > > > ----- file1.c ------
    > > > extern unsigned abc();

    > >
    > > This prototype does not indicate 'no arguments'.

    >
    > It's not a prototype (in C.) It's only a function declaration.


    You're right, I should have written 'declaration'.

    -Mike
     
    Mike Wahler, Jan 20, 2005
    #6
  7. Neo

    Neo Guest

    "Peter Nilsson" <> wrote in message
    news:...
    > Mike Wahler wrote:
    >> "Neo" <> wrote...
    >> > Why the following code is compilable? The function abc() doesn't
    >> > take any arguments, still i can call the function with arbitraty
    >> > number of arguments.
    >> > Even compiler doesn't show any warning. What the standard says?
    >> >
    >> > ----- file1.c ------
    >> > extern unsigned abc();

    >>
    >> This prototype does not indicate 'no arguments'.

    >
    > It's not a prototype (in C.) It's only a function declaration.


    What is the difference between a function declaration and a function
    prototype?

    >
    >> It indicates 'arguments not specified'. It's
    >> also a fragile way to do things.

    >
    > --
    > Peter
    >


    -Neo
     
    Neo, Jan 20, 2005
    #7
  8. Neo

    Chris Torek Guest

    In article <>
    Neo <> wrote:
    >What is the difference between a function declaration and a function
    >prototype?


    To be a prototype, you have to have at least one type-name inside
    the parentheses:

    int zorg(); /* declaration but not prototype */
    int zorg(void); /* declaration and prototype */

    The empty parentheses syntax exists for compatibility with pre-1989
    C code. It is best not to use it in any code written since 1989,
    because prototypes supply more information, so that compilers can
    check the actual calls to the functions.
    --
    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, Jan 20, 2005
    #8
  9. Neo

    Jack Klein Guest

    On Thu, 20 Jan 2005 08:45:59 +0530, "Neo"
    <> wrote in comp.lang.c:

    >
    > "Peter Nilsson" <> wrote in message
    > news:...
    > > Mike Wahler wrote:
    > >> "Neo" <> wrote...
    > >> > Why the following code is compilable? The function abc() doesn't
    > >> > take any arguments, still i can call the function with arbitraty
    > >> > number of arguments.
    > >> > Even compiler doesn't show any warning. What the standard says?
    > >> >
    > >> > ----- file1.c ------
    > >> > extern unsigned abc();
    > >>
    > >> This prototype does not indicate 'no arguments'.

    > >
    > > It's not a prototype (in C.) It's only a function declaration.

    >
    > What is the difference between a function declaration and a function
    > prototype?
    >
    > >
    > >> It indicates 'arguments not specified'. It's
    > >> also a fragile way to do things.

    > >
    > > --
    > > Peter
    > >

    >
    > -Neo


    I already posted an extensive answer to your original post in
    comp.arch.embedded.

    In the future, if you are going to post the same article to multiple
    groups, after making sure that it is topical in the multiple groups,
    cross-post it, that is put all the newsgroup names in a single post.

    As to this question, a function prototype specifies the name of a
    function, the type it returns, and the number and types of arguments:

    int fred(double ricky, char *ethel);

    ....is a prototype, as is:

    int fred(double, char *);

    ....because names for the arguments are optional except when you define
    the body of a function.

    Every line that specifies a function name, return type, and a pair of
    parentheses is a function declaration. If it also includes the types
    of the parameters, it is a prototype.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 20, 2005
    #9
  10. Neo

    Neo Guest

    "Jack Klein" <> wrote in message
    news:...
    > On Thu, 20 Jan 2005 08:45:59 +0530, "Neo"
    > <> wrote in comp.lang.c:
    >
    >>
    >> "Peter Nilsson" <> wrote in message
    >> news:...
    >> > Mike Wahler wrote:
    >> >> "Neo" <> wrote...
    >> >> > Why the following code is compilable? The function abc() doesn't
    >> >> > take any arguments, still i can call the function with arbitraty
    >> >> > number of arguments.
    >> >> > Even compiler doesn't show any warning. What the standard says?
    >> >> >
    >> >> > ----- file1.c ------
    >> >> > extern unsigned abc();
    >> >>
    >> >> This prototype does not indicate 'no arguments'.
    >> >
    >> > It's not a prototype (in C.) It's only a function declaration.

    >>
    >> What is the difference between a function declaration and a function
    >> prototype?
    >>
    >> >
    >> >> It indicates 'arguments not specified'. It's
    >> >> also a fragile way to do things.
    >> >
    >> > --
    >> > Peter
    >> >

    >>
    >> -Neo

    >
    > I already posted an extensive answer to your original post in
    > comp.arch.embedded.
    >
    > In the future, if you are going to post the same article to multiple
    > groups, after making sure that it is topical in the multiple groups,
    > cross-post it, that is put all the newsgroup names in a single post.


    O'kay, Thanks JK.

    >
    > As to this question, a function prototype specifies the name of a
    > function, the type it returns, and the number and types of arguments:
    >
    > int fred(double ricky, char *ethel);
    >
    > ...is a prototype, as is:
    >
    > int fred(double, char *);
    >
    > ...because names for the arguments are optional except when you define
    > the body of a function.
    >
    > Every line that specifies a function name, return type, and a pair of
    > parentheses is a function declaration. If it also includes the types
    > of the parameters, it is a prototype.
    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Neo, Jan 20, 2005
    #10
  11. Neo

    sushant Guest

    if you dont want to pass any arguments then u have to
    explicitly mention the keyword void with the function name
    i.e. func( void ), otherwise it'll take any type n any num of
    arguments
    .. this is C89 but in C99 it'll give u the error as u were
    expecting ...
     
    sushant, Jan 20, 2005
    #11
    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. Elaine Jackson

    number of arguments a function takes

    Elaine Jackson, Jan 29, 2004, in forum: Python
    Replies:
    6
    Views:
    375
    Corey Coughlin
    Jan 30, 2004
  2. =?utf-8?B?5YiY5piK?=
    Replies:
    4
    Views:
    454
    Bo Persson
    Jul 26, 2007
  3. grbgooglefan
    Replies:
    2
    Views:
    431
    Pascal Bourguignon
    Jan 30, 2008
  4. grbgooglefan
    Replies:
    4
    Views:
    447
    Kenny McCormack
    Jan 30, 2008
  5. grbgooglefan
    Replies:
    0
    Views:
    398
    grbgooglefan
    Jan 30, 2008
Loading...

Share This Page