Eric Sosman said:
"Unnecessary" is not always the same as "undesirable" --
do you use indentation to indicate block nesting? -- and the
question of whether to use or omit these casts is in some
degree a matter of taste. Myself, I'd omit them. Yet the
Ten Commandments For C Programmers takes the opposite view
(Third Commandment), so I am probably in a state of sin.
Reference: <
http://www.lysator.liu.se/c/ten-commandments.html>,
written by Henry Spencer.
The original version of this fine document was written, I believe,
before the existence of ANSI C. The Third Commandment is:
Thou shalt cast all function arguments to the expected type if
they are not of that type already, even when thou art convinced
that this is unnecessary, lest they take cruel vengeance upon thee
when thou least expect it.
The newer annotation reads, in part:
It may be thought that the radical new blessing of ``prototypes''
might eliminate the need for caution about argument types. Not so,
brethren. Firstly, when confronted with the twisted strangeness of
variable numbers of arguments, the problem returns... and he who
has not kept his faith strong by repeated practice shall surely
fall to this subtle trap. Secondly, the wise men have observed
that reliance on prototypes doth open many doors to strange
errors, and some indeed had hoped that prototypes would be decreed
for purposes of error checking but would not cause implicit
conversions. Lastly, reliance on prototypes causeth great
difficulty in the Real World today, when many cling to the old
ways and the old compilers out of desire or necessity, and no man
knoweth what machine his code may be asked to run on tomorrow.
This was good advice *at the time it was written*. Today, C compilers
that don't support prototypes are vanishingly rare, and this advice is
IMHO obsolete. You still need to be careful when calling variadic
functions; for example, when passing a pointer to printf with the "%p"
format, you should cast it to void*. (Quibbles: If it's already
void*, you don't need to cast it; if it's char*, you can probably get
away without casting it, but you should anyway; if it's a function
pointer, you can't portably print it with "%p" with or without a
cast.)
I'd say the 4th and 9th commandments are similarly obsolete. The 8th
(which mandates K&R brace style) is something that I *wish* everyone
obeyed, but if you're working under a coding standard that requires
some other style, it's more important to be consistent with that, and
with the existing code, than to use the (IMHO better) K&R style. The
only thing worse than code that uses an ugly brace style is code that
uses a mixture of different brace styles.
The remaining Commandments are still good advice today.
In particular, the Eight Commandment includes one of the best pieces
of programming advice I've ever seen, even if you disagree with the
premise that K&R defined the One True Brace Style:
... for thy creativity is better used in solving problems than in
creating beautiful new impediments to understanding.