Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!mit-eddie!uw-beaver!tektronix!midas!copper!timc From: timc@copper.UUCP Newsgroups: comp.lang.c Subject: ANSI C awkward examples Message-ID: <1470@copper.TEK.COM> Date: Wed, 18-Nov-87 16:36:10 EST Article-I.D.: copper.1470 Posted: Wed Nov 18 16:36:10 1987 Date-Received: Sat, 28-Nov-87 13:29:23 EST Reply-To: timc@copper.UUCP (Tim Carver) Organization: Tektronix, Inc., Beaverton, OR. Lines: 32 I tried to post this a few days ago, and I don't think it got out. My apologies to anyone who gets this twice. Fun things to know about ANSI C: 1) In ANSI C, the usual arithmetic conversions treat int expressions differently from long expressions, even when the sizes of int and long are the same. According to 3.2.1.5: unsigned int u = 4000000000; /* Assume 32 bit int and long */ (u + 1) > 50 is true; but (u + 1L) > 50 is false. 2) This code fragment is not portable in ANSI C: u_long = u_char_1 * u_char_2; According to 3.2.1.1, the multiply is to be: ((int) u_char_1) * ((int) u_char_2) instead of ((unsigned int) u_char_1) * ((unsigned int) u_char_2). For a typical 8086 implementation (32 bit long, 16 bit int) if u_char_1 and u_char_2 each contain 200, the unsigned long result in u_long is 4,294,941,760, instead of the correct 40,000. Unfortunately, this is not due to some sort of typo. ANSI is aware of this example.