Path: utzoo!attcan!uunet!wuarchive!mailrus!uflorida!mephisto!prism!fsu!gw.scri.fsu.edu"!"pepke From: pepke@gw.scri.fsu.edu ("Eric Pepke") Newsgroups: comp.sys.mac.programmer Subject: Re: THINK C bug (possible explanation of why it exists) Message-ID: <501@fsu.scri.fsu.edu> Date: 8 Feb 90 22:03:55 GMT Sender: news@fsu.scri.fsu.edu Organization: "Florida State University" Lines: 43 References:<1990Feb7.225358.1125@oracle.com> <499@fsu.scri.fsu.edu> In article <499@fsu.scri.fsu.edu> I write: > The general idea of parsing the number and then negating it is a pretty > common way of doing things, but creating a negative constant should be the > job of the lexical analyzer, not the parser. You basically look at the Sorry; this is wrong. It has been pointed out to me that the C specification for an integer constant is a sequence of digits. More for those who care: K&R 2, section A2, p. 193: "The type of an integer constant depends on its form, value, and suffix... If it is unsuffixed and decimal, it has the first of these types in which its value can be represented: int, long int, unsigned long int." K&R 2, section A6, p. 204: "The negative of an unsigned quantity is computed by subtracting the promoted value from the largest value of the promoted type and adding one; but negative zero is zero." K&R 2, section A6, p. 197: "When any integer is converted to a signed type, the value is unchanged if it can be represented in the new type and is implementation-defined otherwise." By these rules, 2,147,483,648 should have the type unsigned long int, as it is the first in the list that works. Negation on this should produce 2,147,483,648. Conversion to a signed long is implementation-defined. The most reasonable is just to use the same bits, resulting in -2,147,483,648. THINK C's behavior is, however, consistent with K&R 1, which does not specify a way of making decimal unsigned constants, either by default or explicitly with a U suffix. Eric Pepke INTERNET: pepke@gw.scri.fsu.edu Supercomputer Computations Research Institute MFENET: pepke@fsu Florida State University SPAN: scri::pepke Tallahassee, FL 32306-4052 BITNET: pepke@fsu Disclaimer: My employers seldom even LISTEN to my opinions. Meta-disclaimer: Any society that needs disclaimers has too many lawyers.