type cast in initializer
Doug Gwyn
gwyn at brl-smoke.ARPA
Mon Feb 10 13:19:13 AEST 1986
In article <269 at spp3.UUCP> ansok at spp3.UUCP (Gary Ansok) asks:
> Are there any other cases where casts are NEEDED besides:
>
> function calls: doub_var = sqrt((double) int_var);
> pointer punning: long_var = *(long *) char_ptr;
Type casts are also useful in expressions, to avoid introducing
a temporary variable just to accomplish a type conversion. One
example is casting a (char) to an (unsigned char) before putting
it into an (int) variable, to prevent sign-extension. Another
is casting a (long) to an (unsigned long) before a right-shift,
to avoid sign-propagation. A third case is converting an (int)
to a (long) early in an integer expression, to ensure that the
operations will not overflow.
But it is true that type casts should be used sparingly, and
never to compensate for sloppiness in declaring data types.
C note of the day: 'x' is of type (int), not (char).
More information about the Comp.lang.c
mailing list