Valid C syntax.

B

Ben Bacarisse

[I was inclined to snip all this since I can't see the connection, but
that could be because I just don't get your point. I'll leave it in,
in case your remarks do apply to what I said.]
I think it's interesting that OP had said upthread:
"However some one said to me it is not valid C syntax as
same variable cannot be l-value and r-value in same statement."

I think the actual case is that the
same *expression* cannot be l-value and r-value in same statement,
which is something that I don't think that I had ever considered
before.

a=b=c;

means the exact same thing as:

((a) = ((b) = (c)));

Er, yes, but so what? I don't get the point you are making. The
standard says which forms of expression are lvalues and which are not,
but lvalues can be used in non-lvalue ways. In the above, 'c' is an
lvalue sub-expression but it is used in a way that does not depend on
it being one.
 
C

Chris Dollin

Starfish said:
My favorite one liner is:
a ^= b ^= a ^= b;

`a = a++;` is more compact, uses fewer variables, and is less restricted
on the type of the variable it does use.

--
"It took a very long time, much longer than the most /Sector General/
generous estimates." - James White

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
E

emoh

`a = a++;` is more compact, uses fewer variables, and is less restricted
on the type of the variable it does use.

--
"It took a very long time, much longer than the most           /Sector General/
 generous estimates."                                             - James White

Hewlett-Packard Limited                                          registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN          690597 England

Well a little more confusion. Isn't 'a = a++' equivalent to 'a = a = a
+1' and which I get from the discussion is not correct.
-Paresh
 
J

jameskuyper

emoh said:
....
Well a little more confusion. Isn't 'a = a++' equivalent to 'a = a = a
+1' and which I get from the discussion is not correct.

Yes. That's precisely the point. Starfish's "favorite one liner" has
undefined behavior. What Chris was saying was a joke, suggesting that
if you want to write code with undefined behavior, a=a++ is a better
way to do so, for the reasons he gave. Richard Heathfield made a
similar point, though his example had a constraint violation, whereas
Chris's example does not. CBFalconer just said that it was invalid,
which is more direct, but less fun.
 
R

Richard Tobin

pete said:
Actually, I think that (a = a = a + 1)
also does violate that rule.

As does a=a=a, though I doubt either of them would give the "wrong"
answer in practice.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top