Assignment Ops and Side Effects
Steve Summit
scs at adam.mit.edu
Fri Apr 5 11:24:13 AEST 1991
In article <11805 at dog.ee.lbl.gov> torek at elf.ee.lbl.gov (Chris Torek) writes:
>In article <1991Apr4.202314.961 at csrd.uiuc.edu> bliss at sp64.csrd.uiuc.edu
>(Brian Bliss) notes that on at least one system:
>> char ch;
>> sizeof (ch += 1) == 4
>> sizeof (ch++) == 1
>>apparrently operands of ++ and -- do not undergo integral promotion
>
>...from a reasonably abstract point of view,
> ch++
>and
> ch += 1
>should have the same type. Gcc 1.39, for instance, makes them the same.
As does pcc.
Note that the bug is not that "operands of ++ and -- do not
undergo integral promotion," but rather that the result of an
assignment operator apparently does.
X3.159 sections 3.3.2.4 and 3.3.3.1 say (of postfix and prefix,
respectively, increment and decrement operators) that we are to
"See the discussions of additive operators and compound
assignment for information on... types..." Section 3.3.3.1
additionally asserts that "The expression ++E is equivalent to
(E+=1)." Finally, section 3.3.16 says that "The type of an
assignment expression is the type of the left operand..."
Having just thought about this issue in a C tool I'm working on
(which happens to get the same answer as pcc and gcc in this
case), I can say that it's not terribly hard to get right.
Default promotions are (or ought to be) handled fairly
deliberately and explicitly inside a translater. For each
operator, you ask yourself, "which promotions should I apply
here?" For ++ and --, you don't apply any promotions, because
the operands are (and must remain) lvalues. For += (and the
other op= operators), although promotions may come into play when
evaluating the right-hand side and performing the operation,
since the value is that of the left-hand side (that is, the
coerced value, after assignment), it's reasonably obvious that
the type should be, too.
For the compiler that thinks that sizeof(c += 1) is sizeof(int),
I wonder what it thinks about sizeof(c = 1) or sizeof(c = 1.0) ?
Steve Summit
scs at adam.mit.edu
More information about the Comp.lang.c
mailing list