Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Function returning int instead of char (http://www.velocityreviews.com/forums/t740561-re-function-returning-int-instead-of-char.html)

Navaneeth 12-21-2010 02:02 PM

Re: Function returning int instead of char
 
> 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

Ben Bacarisse 12-21-2010 08:00 PM

Re: Function returning int instead of char
 
Navaneeth <navaneethkn@gmail.com> 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.

Keith Thompson 12-22-2010 01:01 AM

Re: Function returning int instead of char
 
Ben Bacarisse <ben.usenet@bsb.me.uk> 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) kst-u@mib.org <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"


All times are GMT. The time now is 04:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.