473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Valid C syntax.

Is this the valid C statement.

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

-Paresh
Nov 21 '08 #1
14 5795
Hi

On Fri, 21 Nov 2008 03:05:21 -0800, paresh wrote:
Is this the valid C statement.

int a,b,c;
c = 5;
<<<
a = b = c;
>>
No. >>is a parse error.
Can anyone throw the light on this.
Is it the output of some kind of diff?

viza
Nov 21 '08 #2
On 21 Nov, 11:05, paresh <pareshvarsh...@gmail.comwrote:
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

Nov 21 '08 #3
paresh <pa************@gmail.comwrites:
Is this the valid C statement.

int a,b,c;
c = 5;
<<<
a = b = c;
>>>>

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.
Nov 21 '08 #4
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
Nov 21 '08 #5
On Fri, 21 Nov 2008 03:38:16 -0800, paresh wrote:
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
Nov 21 '08 #6
paresh wrote:
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

Nov 21 '08 #7
paresh <pa************@gmail.comwrites:
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.
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.
Nov 21 '08 #8
On Fri, 21 Nov 2008 12:13:09 GMT,
viza <to******@gm-il.com.obviouschange.invalidwrote:
On Fri, 21 Nov 2008 03:38:16 -0800, paresh wrote:
>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.
|
Nov 21 '08 #9
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
Nov 21 '08 #10
On Nov 21, 5:19*pm, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
paresh <pareshvarsh...@gmail.comwrites:
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.
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
Nov 21 '08 #11
paresh wrote:
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.
Nov 21 '08 #12
paresh wrote:
....
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’’."
Dont you think "b" is behaving as a lvalue and rvalue(whatever it is
called).
No. The rvalue is (b=c), not b itself.
Nov 21 '08 #13
paresh wrote:
[...]
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.
Nov 21 '08 #14
bl********@gishpuppy.com (blargg) writes:
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_
Nov 21 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Hill | last post by:
Is this a valid sql statement: SELECT case field1 when 'first' then 1 when 'second' then 1 else null end FROM mytable
2
by: DFS | last post by:
Version 1: -------------------------------- If Condition = A UPDATE query 1 UPDATE query 2 ELSE IF Condition = B UPDATE query
4
by: romek chronowski | last post by:
Hi I'm looking for a code in c or c++ that chcecks math syntax e.g it should check that sin(x+2) is valid syntax but sin(x-+y) or sin((x) is invalid .Thanks in advance (it is very important to me)
7
by: MLH | last post by:
380 If Not IsNull(Me!MatlsList) And (Me!MatlsCost = 0 Or IsNull(Me!MatlsCost)) Then 390 MyMsg = "You entered something in the field entitled 'Materials List' but no charges in the 'Materials'...
8
by: Emanuele Blanco | last post by:
We all know that p->item is the most common syntax for (*p).item (or am I wrong ? ^^). However, if p isn't a struct * but a struct **, what's the correct way to choose a member of the...
4
by: ben | last post by:
getting a bit confused with the details of how c's grammar is specified, especially when you get self-reference like in this: postfix-expression: primary-expression postfix-expression ...
4
by: lizhuo | last post by:
hi all: I reading "C++ Templates: The Complete Guide " Part II: Templates in Depth he write: template<typename T> class B { public: enumE{e1=6,e2=28,e3=496}; virtual void zero(E e = e1);
1
by: Anonymous | last post by:
I implementing the 'virtual constructor' idiom, I have a retrieve() method on an interface, which retrieves an object from storage. I am using this line to cast "up": MyObject::retrieve() {...
2
by: keydrive | last post by:
* html body { text-align: center; } Does the star before the html do anything here or just something that didn't get cleaned up from commenting it out at one point or another. Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.