Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!bu.edu!snorkelwacker.mit.edu!bloom-picayune.mit.edu!news From: scs@adam.mit.edu (Steve Summit) Newsgroups: comp.lang.c Subject: Re: Assignment Ops and Side Effects Message-ID: <1991Apr5.012413.24124@athena.mit.edu> Date: 5 Apr 91 01:24:13 GMT References: <1991Apr3.173046.2367@mccc.edu> <1991Apr4.202314.961@csrd.uiuc.edu> <11805@dog.ee.lbl.gov> Sender: news@athena.mit.edu (News system) Reply-To: scs@adam.mit.edu Organization: Thermal Technologies, Cambridge, MA Lines: 47 In article <11805@dog.ee.lbl.gov> torek@elf.ee.lbl.gov (Chris Torek) writes: >In article <1991Apr4.202314.961@csrd.uiuc.edu> bliss@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@adam.mit.edu