Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!ulysses!gatech!bbn!keesan From: keesan@bbn.UUCP Newsgroups: comp.lang.c Subject: Re: ANSI C awkward examples Message-ID: <4924@cc5.bbn.COM> Date: Wed, 25-Nov-87 14:50:50 EST Article-I.D.: cc5.4924 Posted: Wed Nov 25 14:50:50 1987 Date-Received: Sun, 29-Nov-87 07:44:20 EST References: <1470@copper.TEK.COM> Reply-To: keesan@bbn.com (Morris M. Keesan) Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 51 Keywords: conversion unsigned long Summary: arithmetic conversion rules have changed In article <1470@copper.TEK.COM> timc@copper.UUCP (Tim Carver) writes: > >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. > Not any more. I pointed this out in a formal comment on the first Draft, and the November 9, 1987 Draft (Doc. No: X3J11/87-221) now says in 3.2.1.5: [stuff about double and float] Otherwise, the integral promotions are performed on both operands. Then | the following rules are applied: | If either operand has type unsigned long int, the other operand is converted to unsigned long int. Otherwise, if one operand has type long int and the other has type + unsigned int, if a long int can represent all values of an unsigned + int, the operand of type unsigned int is converted to long it; + otherwise both operands are converted to unsigned long int. + Otherwise, if either operand has type long int, the other operand is converted to long int. Otherwise, if either operand has type unsigned int, the other operand | is converted to type unsigned int. Otherwise, both operands have type int. + indicates new language, | indicates changed language. In the example given, this would result in (u + 1) > 50 ==> (u + 1U) > 50 ==> 4000000001U > 50 ==> 400000001U > 50U ==> 1 and (u + 1L) > 50 ==> ((unsigned long int)u + 1LU) > 50 ==> 4000000001LU > 50 ==> 400000001LU > 50LU ==> 1 -- Morris M. Keesan keesan@bbn.com {harvard,decvax,ihnp4,etc.}!bbn!keesan