Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Function returning int instead of char

Reply
Thread Tools

Re: Function returning int instead of char

 
 
Navaneeth
Guest
Posts: n/a
 
      12-21-2010
> 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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-21-2010
Navaneeth <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-22-2010
Ben Bacarisse <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Function returning int instead of char Navaneeth C Programming 3 12-21-2010 07:31 PM
Re: Function returning int instead of char BartC C Programming 2 12-20-2010 07:47 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments