[comp.lang.c] Compound Assignments

tim@proton.amd.com (Tim Olson) (04/07/91)

In article <1991Apr4.205257.15205@mccc.edu> pjh@mccc.edu (Peter J. Holsberg) writes:
| Where/how does the standard explain that an expression such as
| 
| 	x[i++] *= y;
| 
| has the "x[i++]" part evaluated only once, while an expression such as
| 
| 	x *= y;
| 
| has the "x" part evaluated twice, as in
| 
| 	x = x * y;

In the second example, "x" is not evaluated twice -- it is evaluated
only once, just as in the first example.  The standard says just this
in 3.3.16.2 (Compound assignment):

  Semantics

	A compound assignment of the form E1 op= E2 differs from the
	simple assignment expression E1 = E1 op (E2) only in that the
	lvalue E1 is evaluated only once.


--
	-- Tim Olson
	Advanced Micro Devices
	(tim@amd.com)

darcy@druid.uucp (D'Arcy J.M. Cain) (04/08/91)

In article <1991Apr6.195901.25255@dvorak.amd.com> Tim Olson writes:
>| 	x *= y;
>| 	x = x * y;
>In the second example, "x" is not evaluated twice -- it is evaluated
>only once, just as in the first example.  The standard says just this
>in 3.3.16.2 (Compound assignment):
>	A compound assignment of the form E1 op= E2 differs from the
>	simple assignment expression E1 = E1 op (E2) only in that the
>	lvalue E1 is evaluated only once.

Huh?  Am I missing something or does that say that the two expressions
*ARE* evaluated differently?  What it says is that the number of times
x is evaluated is the *only* difference but it is a difference.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871                    |

pjh@mccc.edu (Pete Holsberg) (04/09/91)

In article <1991Apr6.195901.25255@dvorak.amd.com> tim@amd.com (Tim Olson) writes:
=In article <1991Apr4.205257.15205@mccc.edu> pjh@mccc.edu (Peter J. Holsberg) writes:
=| 	x *= y;
=| 
=| has the "x" part evaluated twice, as in
=| 
=| 	x = x * y;
=
=In the second example, "x" is not evaluated twice -- it is evaluated
=only once, just as in the first example.  The standard says just this
=in 3.3.16.2 (Compound assignment):
=
=  Semantics
=
=	A compound assignment of the form E1 op= E2 differs from the
=	simple assignment expression E1 = E1 op (E2) only in that the
=	lvalue E1 is evaluated only once.

The reference to E1 is ambiguous, as is the entire statement, IMHO. 
*Which* E1 is evaluated "only once", the E1 op= E2 one or the other? 
Does it follow that the remaining one is evaluated twice?  not at all?

Thanks,
Pete
-- 
Prof. Peter J. Holsberg      Mercer County Community College
Voice: 609-586-4800          Engineering Technology, Computers and Math
UUCP:...!princeton!mccc!pjh  1200 Old Trenton Road, Trenton, NJ 08690
Internet: pjh@mccc.edu	     Trenton Computer Festival -- 4/20-21/91

gwyn@smoke.brl.mil (Doug Gwyn) (04/09/91)

In article <1991Apr7.185259.12709@druid.uucp> darcy@druid.uucp (D'Arcy J.M. Cain) writes:
>In article <1991Apr6.195901.25255@dvorak.amd.com> Tim Olson writes:
>>| 	x *= y;
>>| 	x = x * y;
>>In the second example, "x" is not evaluated twice ...
>Huh?  Am I missing something ...

Yes, what you missed was that there were two examples given of compound
assignment, one involving a relatively complex l.h.s. and the other as
shown in the citation above.  The "second example" was the entire
citation, not the second line of the citation.

bhoughto@nevin.intel.com (Blair P. Houghton) (04/09/91)

In article <1991Apr7.185259.12709@druid.uucp> darcy@druid.uucp (D'Arcy J.M. Cain) writes:
>In article <1991Apr6.195901.25255@dvorak.amd.com> Tim Olson writes:
>>in 3.3.16.2 (Compound assignment):
>>	A compound assignment of the form E1 op= E2 differs from the
>>	simple assignment expression E1 = E1 op (E2) only in that the
>>	lvalue E1 is evaluated only once.
>
>Huh?  Am I missing something or does that say that the two expressions
>*ARE* evaluated differently?  What it says is that the number of times
>x is evaluated is the *only* difference but it is a difference.

You must be missing something, because it clearly says they are
evaluated differently and you shouldn't be confused about that.

But the original expressions didn't involve any side effects
other than the assignment itself, so no difference results.

Obviously, a confluence of side-effects creates mass hysteria
between the two:

	*(f(i++)) *= 2;				/* cool */
	*(f(i++)) = *(f(i++)) * 2;		/* cholesterol */

				--Blair
				  "The cow operator tends to
				   chew things of type cud
				   more than once..."

darcy@druid.uucp (D'Arcy J.M. Cain) (04/09/91)

In article <1991Apr7.185259.12709@druid.uucp> I wrote:
>In article <1991Apr6.195901.25255@dvorak.amd.com> Tim Olson writes:
>>| 	x *= y;
>>| 	x = x * y;
>>In the second example, "x" is not evaluated twice -- it is evaluated
>>only once, just as in the first example.  The standard says just this
> [me pointing out the obvious]

Oops.  There was a first example that I missed when I posted.  The above
were examples 2 and 3.  Sorry Tim.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871                    |

tim@proton.amd.com (Tim Olson) (04/09/91)

In article <1991Apr7.185259.12709@druid.uucp> darcy@druid.uucp (D'Arcy J.M. Cain) writes:
| In article <1991Apr6.195901.25255@dvorak.amd.com> Tim Olson writes:
| >| 	x *= y;
| >| 	x = x * y;
| >In the second example, "x" is not evaluated twice -- it is evaluated
| >only once, just as in the first example.  The standard says just this
| >in 3.3.16.2 (Compound assignment):
| >	A compound assignment of the form E1 op= E2 differs from the
| >	simple assignment expression E1 = E1 op (E2) only in that the
| >	lvalue E1 is evaluated only once.
| 
| Huh?  Am I missing something or does that say that the two expressions
| *ARE* evaluated differently?

Yes, the two expressions you show are evaluated differently. I think
the confusion here is that the two expressions you show aren't the two
original expressions I was talking about -- they were (from the
original article):

|       x[i++] *= y;
|
| has the "x[i++]" part evaluated only once, while an expression such as
|
|       x *= y;
|
| has the "x" part evaluated twice, as in

Try substituting these two expressions into the discussion above, it
should make more sense.


--
	-- Tim Olson
	Advanced Micro Devices
	(tim@amd.com)

gwyn@smoke.brl.mil (Doug Gwyn) (04/10/91)

In article <1991Apr8.174951.22448@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes:
>=	A compound assignment of the form E1 op= E2 differs from the
>=	simple assignment expression E1 = E1 op (E2) only in that the
>=	lvalue E1 is evaluated only once.
>The reference to E1 is ambiguous, as is the entire statement, IMHO. 
>*Which* E1 is evaluated "only once", the E1 op= E2 one or the other? 
>Does it follow that the remaining one is evaluated twice?  not at all?

You have GOT to be kidding -- there is nothing at all ambiguous about
the quoted specification.  It is an elegant way of expressing precisely
the semantics for op=.  I suggest you study it until enlightenment
suddenly dawns upon you.

Wu.

pjh@mccc.edu (Pete Holsberg) (04/12/91)

In article <15776@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
=In article <1991Apr8.174951.22448@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes:
=>=	A compound assignment of the form E1 op= E2 differs from the
=>=	simple assignment expression E1 = E1 op (E2) only in that the
=>=	lvalue E1 is evaluated only once.
=>The reference to E1 is ambiguous, as is the entire statement, IMHO. 
=>*Which* E1 is evaluated "only once", the E1 op= E2 one or the other? 
=>Does it follow that the remaining one is evaluated twice?  not at all?
=
=You have GOT to be kidding -- there is nothing at all ambiguous about
=the quoted specification.  It is an elegant way of expressing precisely
=the semantics for op=.  I suggest you study it until enlightenment
=suddenly dawns upon you.

Nope!  Not at all.  If the lvalue E1 in "E1 op= E2" is "....evaluated
only once...", how many times is the lvalue E1 in "E1 = E1 op E2"
evaluated?  If the answer is "once", why do they say "... ONLY [emphasis
added] once..."

Suppose the lvalue E1 is, say, "x" in both cases.  How does it get
evaluated AS AN LVALUE in the second situation?

Thank you for your patience, oh master.  Your humble student awaits
enlightenment from your keyboard.  ;-)

Pete
-- 
Prof. Peter J. Holsberg      Mercer County Community College
Voice: 609-586-4800          Engineering Technology, Computers and Math
UUCP:...!princeton!mccc!pjh  1200 Old Trenton Road, Trenton, NJ 08690
Internet: pjh@mccc.edu	     Trenton Computer Festival -- 4/20-21/91

gwyn@smoke.brl.mil (Doug Gwyn) (04/16/91)

In article <1991Apr11.183942.2195@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes:
>In article <15776@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>=In article <1991Apr8.174951.22448@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes:
>=>=	A compound assignment of the form E1 op= E2 differs from the
>=>=	simple assignment expression E1 = E1 op (E2) only in that the
>=>=	lvalue E1 is evaluated only once.
>=>The reference to E1 is ambiguous, as is the entire statement, IMHO. 
>=>*Which* E1 is evaluated "only once", the E1 op= E2 one or the other? 
>=>Does it follow that the remaining one is evaluated twice?  not at all?
>=You have GOT to be kidding -- there is nothing at all ambiguous about
>=the quoted specification.  It is an elegant way of expressing precisely
>=the semantics for op=.  I suggest you study it until enlightenment
>=suddenly dawns upon you.
>Nope!  [drivel deleted]

The point I was emphasizing is that you should be able to "grok" the
description with practically no effort.  If you're having difficulty,
you need to alter your thinking about it until you grok it.  Probably
your problem would be that you're trying to analyze the specification
at a more detailed level than necessary for understanding.

The description presupposes that you already understand what a simple
assignment expression means, and it builds on that knowledge to
explain what a compound assignment expression means.  It does that
by (a) rewriting into terms you should already understand and (b)
stating how the actual case differs from the rewrite.  I would think
that practically any native speaker of English who had already
assimilated the semantics of C through simple assignment expressions
should understand this specification for compound assignment with no
problem at all.  Certainly that's the way that I learned about the
semantics of compound assignment when I was first learning C, and it
was crystal clear to me then.

rkl@cbnewsh.att.com (kevin.laux) (04/16/91)

In article <15823@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes:
> In article <1991Apr11.183942.2195@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes:
> >In article <15776@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
> >=In article <1991Apr8.174951.22448@mccc.edu> pjh@mccc.edu (Pete Holsberg) writes:
> >=>=	A compound assignment of the form E1 op= E2 differs from the
> >=>=	simple assignment expression E1 = E1 op (E2) only in that the
> >=>=	lvalue E1 is evaluated only once.

	[stuff about ambiguity and understanding deleted]

	I think the problem of 'ambiguity' arises from what expressions are
chosen as examples.

	For instance,		(E1 = x)

	x += y		and 	x = x + y

	produce the same result but might cause one to say E1 evaluated only
once?

	Whereas			(E1 = x [i++])

	x [i++] += y	and	x [i++] = x [i++] + y

	do not produce the same result but more clearly illustrates the point
that in compound assignments E1 is evaluated only once.

-- 
________________________________________________________________________________
	R. Kevin Laux				Email: rkl1@hound.att.com
	AT&T Bell Labs				Voice: (908) 949-1160
	Holmdel, NJ 07733			Fax:   (908) 949-0959