🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3515

Re: Integral promotion.

#3515
From: Tanmoy Bhattacharya
Date: Wed, 21 Feb 1996 16:15
54 lines
3236 bytes
In article <4gf99c$1fp@solutions.solon.com> sl14@crux3.cit.cornell.edu
(S. Lee) writes: 
<snip>
   In article <4g55fl$avl@solutions.solon.com>,  <msb@sq.com> wrote:
   >> > short test(short x1, short x2) {
   >> >  short result;
   >> >  result = x1 + x2;
   >> 
   >> The addition operator "+" converts your short operands to ints.
   >
   >No, just using their values does that.  The operator + never sees
   >operands narrower than int.  In the statement
   >	x1; 
   >the short is converted to int before being discarded.

   Okay, I'm confused.  Let just say the above is true.  Then the expression
   "x1 + x2" has type int because x1 and x2 were converted to int before
   being passed to the + operator.

   So what if sizeof(short) == sizeof(int)?  Assuming both x1 and x2 are
   non-negative, would "x1 + x2" be unsigned int if it wouldn't fit in an
   int?  In short, would there be undefined/implementation defined behaviour
   if "result" is unsigned int?  How about long?

Why would x1+x2 become an unsigned int? It is x1 and x2 that are
converted to ints: integral promotions occur on the operands, not on
the result! When you add int's you get an int: and if the result does
not fit, you are in trouble!

Furthermore, what x1 is promoted to does not depend on the _value_ of
x1, but the _type_ of x1. If all values that the type can hold can fit
into an int, it becomes an int: otherwise it becomes an unsigned
int. Thus if x is unsigned short with value 2, USHRT_MAX is greater than
INT_MAX, x is promoted to unsigned int and not to an int, because
there exists some unsigned short value which does not fit into an int,
even though the particular value 2 does. This is a general feature of
C: the type of an expression does not depend on any _evaluation_
(except for evaluation of integral constants, possible at compile
time, to find array dimensions and the like).

The case you are talking of is even simpler. int must be able to hold
all values that a short holds. So, if x1 and x2 are short, they are
promoted to int, and x1+x2 is therefore also an int: and if the result
does not fit, the compiler won't do anything to save you.

Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Technical Details
Message-ID: <4gfgfp$3k8@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4fstj7$2l6@solutions.solon.com> <4fvgvb$e0t@solutions.solon.com> <4g55fl$avl@solutions.solon.com> <4gf99c$1fp@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
Newsgroups: comp.std.c,comp.lang.c.moderated
Subject: Re: Integral promotion.
Date: 21 Feb 1996 10:15:52 -0600
Organization: Los Alamos National Laboratory
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4gfgfp$3k8@solutions.solon.com>
References: <4fstj7$2l6@solutions.solon.com> <4fvgvb$e0t@solutions.solon.com>
 <4g55fl$avl@solutions.solon.com> <4gf99c$1fp@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com