[comp.lang.c] Comma operator: lvalue????

volpe@underdog.crd.ge.com (Christopher R Volpe) (08/08/90)

Can someone with a copy of the standard tell me if the comma
operator produces an lvalue. This is one of the only operators
that K&R2 fails to mention whether or not it returns an lvalue.
My intuition at first would tell me no, but the following works
under GNU C:

main()
{
  int a,b;

  if (&a == &(a,b)) printf("I shouldn't see this\n");
  if (&b == &(a,b)) printf(" Test 1: address-of works properly.\n");
  (a,b)=4;
  if (b==4) printf("Test 2: Assignment works properly.\n");
}

I assume that if the comma operator EVER produces an lvalue, it does
so if and only if the right operand is an lvalue. Is this correct?

(Oh, BTW, sorry I forgot to sign my previous post about the macros)

Chris Volpe
GE Corporate R&D
volpecr@crd.ge.com

ark@alice.UUCP (Andrew Koenig) (08/08/90)

In article <10839@crdgw1.crd.ge.com>, volpe@underdog.crd.ge.com (Christopher R Volpe) writes:

> Can someone with a copy of the standard tell me if the comma
> operator produces an lvalue.

It does in C++ but not in C.  However, it's impossible to write a
strictly conforming C program that tests whether or not your
implementation returns an lvalue from the comma operator, so it
is a permissible extension to do so in C.
-- 
				--Andrew Koenig
				  ark@europa.att.com

diamond@tkou02.enet.dec.com (diamond@tkovoa) (08/09/90)

In article <10839@crdgw1.crd.ge.com> volpe@underdog.crd.ge.com (Christopher R Volpe) writes:

>Can someone with a copy of the standard tell me if the comma
>operator produces an lvalue. This is one of the only operators
>that K&R2 fails to mention whether or not it returns an lvalue.

The last draft of the standard puts it in a FOOTNOTE that the comma operator
does not produce an lvalue.  And because of the positioning of the footnote,
it is very easy to miss.  It is typographically correct but accidentally
ended up in a reader-unfriendly manner, so it looks like even K&R missed it!

>My intuition at first would tell me no, but the following works
>under GNU C:

In some cases, if a compiler provides an extension, it is still required
to issue a warning.  However, it seems that this extension does not require
a warning.  No syntax or constraints section seems to say which expressions
yield lvalues.

>I assume that if the comma operator EVER produces an lvalue, it does
>so if and only if the right operand is an lvalue. Is this correct?

Intuitively it's hard to imagine how the result could be an lvalue if the
right operand isn't an lvalue.  However, when you're asking what's correct,
you have to RTIFM (I == implementation's).

-- 
Norman Diamond, Nihon DEC     diamond@tkou02.enet.dec.com
This is me speaking.  If you want to hear the company speak, you need DECtalk.