Uses of \"short\" ?
VLD/VMB
gwyn at BRL.ARPA
Fri Nov 15 17:14:33 AEST 1985
Your note reminded me of something I wanted to comment on.
I have observed a lot of C code that tries converting back
and forth between data types (using type casts) with wild
abandon. This is symptomatic of a program out of control.
Using appropriate data types in a straight-forward fashion
results in code containing very few type casts. The main
uses I have for type casts fall into a few categories:
(void) to discard the return value of a function;
this should be not be done without thinking about
the appropriateness of ignoring the return value.
I do this mainly for fprintf(stderr,...), since
usually I can't think of anything better to do if
that function call fails.
Casting the returned value from malloc() (also the
argument to free()). This should be obvious.
Forcing a short or an int to become a long, as in
i = j * (long)k / l;
to avoid numeric overflow.
Truncating a floating-point quantity to its integer
part (warning! not the same as the floor() function).
Passing a NULL pointer argument to a function.
Documenting a coercion that will occur anyhow, as
in passing a (char) as a function argument or in
certain assignments, when it is important to
realize that this is happening.
There are some other instances where casts are useful, but
they should be used sparingly and not as a substitute for
declaring data types properly.
More information about the Comp.lang.c
mailing list