MC wrote:
"Barry Schwarz" <sc******@deloz.net> wrote in message
news:bi**********@216.39.134.219... If their is a prototype in scope for the function then the cast is
completely superfluous.
So if I have a header file that has the prototype
for the function and I include that header file
in this source file, then the cast is superfluous .
Am i right ?
In the case presented (where question refers to the
function's first argument), yes.
There's a wrinkle, though, for what are called "variadic
functions." Like the *printf() family, these functions take
a few "mandatory" arguments of known types followed by an
indeterminate number of "variable" arguments. Both the
number and the types of the variable arguments can differ
from one call to the next, e.g.
printf ("Hello, world!\n");
fprintf (stdout, "The answer is %d\n", 42);
sprintf (buff, "%d / %d = %f", 42, 26, 42.0/26.0);
Now, how does all this bear on your question? The
catch is that the compiler has no idea what types the
variable arguments are supposed to have: they can, as
shown above, be different in different calls. Thus, the
compiler does not know it's supposed to convert an argument
from whatever type you present to whatever type the function
actually expects; the compiler doesn't know the expected
type. So for arguments that correspond to the "..." part
of a variadic function's parameter list, casts are still
necessary if the supplied argument doesn't already have
the correct type.
To sum up, if the type of the supplied value doesn't
match the type of the expected argument:
- If there is no prototype in scope, you need a cast.
(Better still would be to introduce a prototype.)
- If there is a prototype in scope and the function
is not variadic, no cast is needed because the
compiler provides the needed conversion. (But if
there's an explicit cast, no harm is done.)
- If there's a prototype and the function is variadic
but the supplied value is for one of the "fixed"
arguments, no cast is needed. (That's why the
first argument is special: there must be at least
one fixed argument, so the very first argument is
necessarily fixed.)
- If there's a prototype and the function is variadic
and the supplied value is for one of the "..."
arguments, you need a cast.
Complicated as all this may seem, it *still* isn't quite
accurate. When the compiler doesn't know what the function
expects (no prototype, or "..." argument), it applies the
"argument promotions." These will, for example, convert a
`float' value to `double' automatically, so some cases of
type mismatch don't actually need the explicit cast mentioned
above.
In light of all these ins and outs, Henry Spencer recommends
explicit casts even when they don't seem to be needed; see the
Third Commandment in
http://www.lysator.liu.se/c/ten-commandments.html
Personally, I confess myself a habitual and wilful sinner against
the Third Commandment, and I will no doubt burn in Hell (or in
gdb, which some people consider different.) But it is probably a
good idea to consider the Commandments carefully before deciding
to violate them: if you're going to sin, do so on purpose and not
by accident.
--
Er*********@sun.com