Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cbosgd!gatech!seismo!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: Re: type cast in initializer Message-ID: <792@brl-smoke.ARPA> Date: Sun, 9-Feb-86 22:19:13 EST Article-I.D.: brl-smok.792 Posted: Sun Feb 9 22:19:13 1986 Date-Received: Wed, 12-Feb-86 07:35:18 EST References: <302@hsi.UUCP> <1450@bbncc5.UUCP> <305@cray.UUCP> <269@spp3.UUCP> Reply-To: gwyn@brl.ARPA Organization: /usr/local/lib/news/organization Lines: 20 In article <269@spp3.UUCP> ansok@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).