Re: Function returning int instead of char

Discussion in 'C Programming' started by Navaneeth, Dec 21, 2010.

  1. Navaneeth

    Navaneeth Guest

    > Finally, the function declarations in the header are not prototypes. C
    > has had proper function prototypes for 20 years.


    Thanks a lot Ben for the reply. It makes sense. But I didn't fully understood what do you meant by the quoted sentense? Can you please explain it more clearly?

    Thanks
     
    Navaneeth, Dec 21, 2010
    #1
    1. Advertising

  2. Navaneeth <> writes:

    >> Finally, the function declarations in the header are not prototypes. C
    >> has had proper function prototypes for 20 years.

    >
    > Thanks a lot Ben for the reply. It makes sense. But I didn't fully
    > understood what do you meant by the quoted sentense? Can you please
    > explain it more clearly?


    Looking again, it turns out to be true of only three of the four
    function declarations.

    Let me explain. A function prototype tells the compiler how many
    arguments should be present in a call to that function. It also
    specifies the types. Due to a quirk in the syntax of C as it was before
    the language was standardised, a declaration such as

    extern int utf8_decode_next();

    is not a prototype. The compiler is not permitted to complain about a
    call like

    utf8_decode_next(42)

    When a function takes no arguments, the parameter list should be the
    keyword "void":

    extern int utf8_decode_next(void);

    This makes the declaration into a prototype so the compiler can (indeed
    it should) complain about an incorrect call.

    --
    Ben.
     
    Ben Bacarisse, Dec 21, 2010
    #2
    1. Advertising

  3. Ben Bacarisse <> writes:
    [...]
    > Let me explain. A function prototype tells the compiler how many
    > arguments should be present in a call to that function. It also
    > specifies the types. Due to a quirk in the syntax of C as it was before
    > the language was standardised, a declaration such as
    >
    > extern int utf8_decode_next();
    >
    > is not a prototype. The compiler is not permitted to complain about a
    > call like
    >
    > utf8_decode_next(42)


    Well, that's not *quite* true. A compiler is permitted to complain
    about anything it likes, and in this case it would be quite reasonable
    to issue a warning if the compiler happens to know that
    utf8_decode_next() doesn't take a single numeric argument. But in my
    (limited) experience, most compilers don't bother; I think the
    assumption is that if you want that kind of checking, you should be
    using prototypes anyway.

    > When a function takes no arguments, the parameter list should be the
    > keyword "void":
    >
    > extern int utf8_decode_next(void);
    >
    > This makes the declaration into a prototype so the compiler can (indeed
    > it should) complain about an incorrect call.


    In fact, it *must* complain (issue a diagnostic message).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 22, 2010
    #3
    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. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    424
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  4. BartC
    Replies:
    2
    Views:
    330
    BartC
    Dec 20, 2010
  5. Navaneeth

    Re: Function returning int instead of char

    Navaneeth, Dec 21, 2010, in forum: C Programming
    Replies:
    3
    Views:
    325
    Ben Pfaff
    Dec 21, 2010
Loading...

Share This Page