Assignment Ops and Side Effects
Christopher R Volpe
volpe at camelback.crd.ge.com
Mon Apr 8 23:14:50 AEST 1991
In article <1991Apr5.234636.22610 at csrd.uiuc.edu>,
bliss at sp64.csrd.uiuc.edu (Brian Bliss) writes:
|>> 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) ?
|>(c is a char)
|>
|>The compiler is Sun 4.0's cc, and indeed, it returns 1 for both cases.
|>So indeed it is the assignment that is responsible for coercing
|>the result back to char type.
|>
|>bb
Could someone explain to me why sizeof(c=1) is 1 and not (typically) 4??
The result of the assignment operator is not an l-value, and thus undergoes
the integral promotions. The result of "c=1" in an expression context
should be promoted to int. For example, for an unprototyped function f,
f(c=1) assigns 1 to the character c, and then passes the INTEGER 1 to f.
Similarly, sizeof(c=1) should see that its operand is a non-lvalue value of
type char (the value is not evaluated) and should thus be promoted to int.
(For sizeof(c), c is an lvalue and does not undergo promotion since it is
an operand of sizeof. (Section 3.2.2.1 paragraph 2))
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com
More information about the Comp.lang.c
mailing list