Connecting Tech Pros Worldwide Forums | Help | Site Map

Valid C syntax.

paresh
Guest
 
Posts: n/a
#1: Nov 21 '08
Is this the valid C statement.

int a,b,c;
c = 5;
<<<
a = b = c;
Quote:
Quote:
Quote:
>>>
Can anyone throw the light on this.

-Paresh

viza
Guest
 
Posts: n/a
#2: Nov 21 '08

re: Valid C syntax.


Hi

On Fri, 21 Nov 2008 03:05:21 -0800, paresh wrote:
Quote:
Is this the valid C statement.
>
int a,b,c;
c = 5;
<<<
a = b = c;
Quote:
Quote:
>>
No. >>is a parse error.
Quote:
Can anyone throw the light on this.
Is it the output of some kind of diff?

viza
Nick Keighley
Guest
 
Posts: n/a
#3: Nov 21 '08

re: Valid C syntax.


On 21 Nov, 11:05, paresh <pareshvarsh...@gmail.comwrote:
Quote:
Is this the valid C statement.
>
int *a,b,c;
c = 5;
<<<
a = b = *c;
>
Can anyone throw the light on this.
it is unclear what you are asking. What is "this"?

1. the declaration and two statements above are not "a valid
statment".
They are several statments.

2. "<<<" is a syntax error

3. "a = b = c;" is a valid statement. Why wouldn't it be?

Note assignment ("=") evaluates right to left so the above
assigns c to b giving a result equal to the new b which is
then assigned to a.

--
Nick Keighley



Ben Bacarisse
Guest
 
Posts: n/a
#4: Nov 21 '08

re: Valid C syntax.


paresh <pareshvarshney@gmail.comwrites:
Quote:
Is this the valid C statement.
>
int a,b,c;
c = 5;
<<<
a = b = c;
Quote:
Quote:
>>>>
>
Can anyone throw the light on this.
Presuming that the "<<<" and ">>>" are just there to mark the bit you
are asking about then, yes, it is valid. Both a and b are assigned
the value of c.

It is parsed as 'a = (b = c);'. The result of the right-hand b = c is
the value that b will have after the assignment and it is that
value that gets assigned to a (after any conversions). This matters
most when the types of a, b and c differ. In fact, in such a case I
would advise against an assignment like this since the effect can be
mildly confusing.

--
Ben.
paresh
Guest
 
Posts: n/a
#5: Nov 21 '08

re: Valid C syntax.


yes "<<<" and ">>>" are for marking.

I want to know that the "a = b = c;" statement is valid syntax
according to ISO C.

I ran it myself on various C/C++ compilers it worked. 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.

-Paresh
viza
Guest
 
Posts: n/a
#6: Nov 21 '08

re: Valid C syntax.


On Fri, 21 Nov 2008 03:38:16 -0800, paresh wrote:
Quote:
int a,b,c;
>
I want to know that the "a = b = c;" statement is valid syntax according
to ISO C.
It is valid but usually stupid to use it.
It assigns the value of c to both a and b.

If the types are different the conversions become more complicated.

viza
Chris Dollin
Guest
 
Posts: n/a
#7: Nov 21 '08

re: Valid C syntax.


paresh wrote:
Quote:
yes "<<<" and ">>>" are for marking.
>
I want to know that the "a = b = c;" statement is valid syntax
according to ISO C.
>
I ran it myself on various C/C++ compilers it worked. 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.
Well, let's note first that in `a = b = c`, all the variables are
different, so even if the same variable couldn't be both an
lvalue and rvalue in one statement, it wouldn't matter.

Let's note next that if "some one" really did say that, they'd have
forgotten about statements like `i = i + 1;` (or `i += 1;` or `i++;`).

What I suspect they said, or thought they were saying, is that you're
not allowed [1] to update a variable /more than once/ in a single statement
(actually, before the next sequence point, but that's another discussion),
and if you update a variable, you can only read it in that same statement
(ditto) /for the purpose of determining its new value/.

So `x = x = 0;` isn't allowed. More subtly, `a[i] = a[j] = 0;` isn't
allowed if `i == j`. Also `i = i + i++;` isn't allowed.

Finally, this isn't a matter of /syntax/ -- how to construct larger
bits of program from smaller bits -- at all. It's a matter of /semantics/,
the meaning of bits of programs. The statement `a[i] = a[j] = 0;` is
syntactically fine. Given appropriate definitions for `a`, `i`, and `j`,
its compile-time semantics is fine too -- no constraint violation.
But to be meaningful when it's executed, `i` must not equal `j`, and
this is not a matter of syntax.

[1] On pain of undefined behaviour, ie, the program's behaviour becomes
unpredictable.

--
"There's no 'we' in 'team'." Unsaid

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

Ben Bacarisse
Guest
 
Posts: n/a
#8: Nov 21 '08

re: Valid C syntax.


paresh <pareshvarshney@gmail.comwrites:
Quote:
yes "<<<" and ">>>" are for marking.
>
I want to know that the "a = b = c;" statement is valid syntax
according to ISO C.
It is valid syntax. What is more, the semantics are well-defined too.
Quote:
I ran it myself on various C/C++ compilers it worked. 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.
First, if this were forbidden (it is not) it would not be classed as
invalid syntax. Syntax (in C) is just about the "shape" of the code;
what sort of things can go where. Second, you were told something the
"same variable" and all yours are different so what was said has
nothing to do with your example. Third, what this person said is
obviously false, unless you think that C forbids:

x = x + 1;

in which x is used both as a lvalue and as an r-value (C does not use
the term rvalue be we can reasonably assume what was meant).

--
Ben.
Martien Verbruggen
Guest
 
Posts: n/a
#9: Nov 21 '08

re: Valid C syntax.


On Fri, 21 Nov 2008 12:13:09 GMT,
viza <tom.viza@gm-il.com.obviouschange.invalidwrote:
Quote:
On Fri, 21 Nov 2008 03:38:16 -0800, paresh wrote:
>
Quote:
>int a,b,c;
>>
>I want to know that the "a = b = c;" statement is valid syntax according
>to ISO C.
>
It is valid but usually stupid to use it.
Why is it stupid?

Martien
--
|
Martien Verbruggen | Begin at the beginning and go on till you
| come to the end; then stop.
|
paresh
Guest
 
Posts: n/a
#10: Nov 21 '08

re: Valid C syntax.


Thanks Chirs for your explanation.

That means "a=b=c;" is valid syntactically and semantically.
However if execution of the statement is right to left then it seems
that it will do what we are expecting.
On the other way (i.e left to right) it will mess up the variable "a".

Is "right to left" or "left to right" execution depends on the
compiler or its a standard?

-Paresh


paresh
Guest
 
Posts: n/a
#11: Nov 21 '08

re: Valid C syntax.


On Nov 21, 5:19*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Quote:
paresh <pareshvarsh...@gmail.comwrites:
Quote:
yes "<<<" and ">>>" are for marking.
>
Quote:
I want to know that the "a = b = c;" statement is valid syntax
according to ISO C.
>
It is valid syntax. *What is more, the semantics are well-defined too.
>
Quote:
I ran it myself on various C/C++ compilers it worked. 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.
>
First, if this were forbidden (it is not) it would not be classed as
invalid syntax. *Syntax (in C) is just about the "shape" of the code;
what sort of things can go where. *Second, you were told something the
"same variable" and all yours are different so what was said has
nothing to do with your example. *Third, what this person said is
obviously false, unless you think that C forbids:
>
* x = x + 1;
>
in which x is used both as a lvalue and as an r-value (C does not use
the term rvalue be we can reasonably assume what was meant).
>
--
Ben.
So what is the term used for rvalue in C?
Dont you think "b" is behaving as a lvalue and rvalue(whatever it is
called).

-Paresh
James Kuyper
Guest
 
Posts: n/a
#12: Nov 21 '08

re: Valid C syntax.


paresh wrote:
Quote:
yes "<<<" and ">>>" are for marking.
>
I want to know that the "a = b = c;" statement is valid syntax
according to ISO C.
>
I ran it myself on various C/C++ compilers it worked. 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.
Note: The C standard uses "lvalue" and "value", rather than "l-value"
and "r-value".

I think the person who told you that may have parsed this statement
incorrectly. I suspect he's thinking about it as if it was the
combination of a=b and b=c in a single statement. There is a rule which
says that, the behavior is undefined if, between two sequence points, a
value is stored in an object, and the stored value of that object is
retrieved for any purpose other than determining what the new value of
that object will be. Therefore, if you combined a=b with b=c in a single
statement without an intervening sequence point, the behavior would be
undefined. Example:

(a=b)*(b=c); // Behavior is undefined.

However, in reality, while a=b=c contains no sequence points, it is not
a combination of a=b with b=c. Instead, it is equivalent to

a = (b=c);

Every assignment expression ( =, +=, -=, *=, etc.) has a value (an
r-value, in your terms) which is the same as the value of it's left
operand after the assignment. Note that the value of an assignment can
be used immediately, even though the actual change to the stored value
of it's left operand doesn't have to happen until just before the next
sequence point. This means that the value of an assignment expression
maybe used even in circumstances, such as this one, where retrieve that
stored value would have undefined behavior.
James Kuyper
Guest
 
Posts: n/a
#13: Nov 21 '08

re: Valid C syntax.


paresh wrote:
....
Quote:
So what is the term used for rvalue in C?
There is exactly one use of the term "rvalue" in the C standard. It
occurs in footnote 53, in section 6.3.2.1p1. It says: "What is sometimes
called ‘‘rvalue’’ is in this International Standard described as the
‘‘value of an expression’’."
Quote:
Dont you think "b" is behaving as a lvalue and rvalue(whatever it is
called).
No. The rvalue is (b=c), not b itself.
blargg
Guest
 
Posts: n/a
#14: Nov 21 '08

re: Valid C syntax.


paresh wrote:
[...]
Quote:
That means "a=b=c;" is valid syntactically and semantically.
However if execution of the statement is right to left then it seems
that it will do what we are expecting.
On the other way (i.e left to right) it will mess up the variable "a".
[...]
Yes, if we executed

(a = b) = c;

we'd have undefined behavior, because we are attempting to modify 'a' more
than once between two sequence points.
Ben Pfaff
Guest
 
Posts: n/a
#15: Nov 21 '08

re: Valid C syntax.


blargg.h4g@gishpuppy.com (blargg) writes:
Quote:
Yes, if we executed
>
(a = b) = c;
>
we'd have undefined behavior, because we are attempting to modify 'a' more
than once between two sequence points.
Such a statement is a constraint violation that requires a
diagnostic, because the result of a assignment operator is not an
lvalue.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Closed Thread