Article View: net.lang.c
Article #9404Re: Promotion of integral types
From: Ray Butterworth
Date: Fri, 26 Sep 1986 18:27
Date: Fri, 26 Sep 1986 18:27
35 lines
2584 bytes
2584 bytes
> It is interesting to note that K&R does not say that unsigned char or > unsigned short get promoted to unsigned int; it says that they get promoted > to integer (on page 183), which is a bit vague. Harbison & Steele says > very clearly what is said above. The ANSI Draft (1986.07.09 version) says > very clearly that unsigned char and unsigned short are promoted to int, > since the values can all be represented in an int (assuming sizeof(int) > is greater than sizeof(short), which is usually true). > > What to do? Frankly, I prefer the ANSI approach, but it does represent a > silent change from what H&S (and Chris Torek) perceive to be standard > practice. For some reason, it is not flagged as such in the rationale > document; it isn't even discussed there. There is one major problem with the ANSI proposal. When doing comparisions, if the two types aren't identical after the usual conversions and one of them is unsigned, the comparison is done as unsigned. e.g. "(-1) >= ui" for all unsigned ints ui. (In particular, "(-1)>sizeof(anything)" is always true if size_t is unsigned long or unsigned int.) This can be somewhat surprising, but one can learn to live with it once the principle is understood. The real problem occurs with shorts and unsigned shorts. Consider the comparison "neg1>=us", where "neg1" is an int with value -1 and "us" is any unsigned short. If sizeof(unsigned short)<sizeof(int), the "us" is promoted to an int and the comparison is done between ints and the result is always false. But if sizeof(unsigned short)==sizeof(int), "us" is promoted to unsigned int, "neg1" is converted to unsigned int, and the comparison is always true. Now this is not only surprising, it means that comparing an unsigned short with an int (or a constant) can give different results depending upon the machine one is using. How many people are going to keep this in mind while writing their code?
Technical Details
Message-ID:
<3405@watmath.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!rbutterworthReferences:
<3594@brl-smoke.ARPA> <1219@drutx.UUCP> <2233@gitpyr.UUCP> <547@chinet.UUCP> <3422@umcp-cs.UUCP> <271@myrias.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site watmath.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!rbutterworth From: rbutterworth@watmath.UUCP (Ray Butterworth) Newsgroups: net.lang.c Subject: Re: Promotion of integral types Message-ID: <3405@watmath.UUCP> Date: Fri, 26-Sep-86 14:27:47 EDT Article-I.D.: watmath.3405 Posted: Fri Sep 26 14:27:47 1986 Date-Received: Mon, 29-Sep-86 06:22:43 EDT References: <3594@brl-smoke.ARPA> <1219@drutx.UUCP> <2233@gitpyr.UUCP> <547@chinet.UUCP> <3422@umcp-cs.UUCP> <271@myrias.UUCP> Organization: U of Waterloo, Ontario Lines: 36