Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!brl-tgr!tgr!gwyn@BRL.ARPA From: gwyn@BRL.ARPA (VLD/VMB) Newsgroups: net.lang.c Subject: Re: Uses of \"short\" ? Message-ID: <3241@brl-tgr.ARPA> Date: Fri, 15-Nov-85 02:14:33 EST Article-I.D.: brl-tgr.3241 Posted: Fri Nov 15 02:14:33 1985 Date-Received: Sat, 16-Nov-85 09:50:27 EST Sender: news@brl-tgr.ARPA Lines: 35 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.