473,608 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5809
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.comwrite s:
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.comwrite s:
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.obviousc hange.invalidwr ote:
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

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

Similar topics

3
4479
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
9650
by: DFS | last post by:
Version 1: -------------------------------- If Condition = A UPDATE query 1 UPDATE query 2 ELSE IF Condition = B UPDATE query
4
2109
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
1198
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' cost field. " 400 MyMsg = MyMsg & "You must enter the amount of the charges in the field provided or leave the description empty." 410 MsgBox MyMsg, 32, "Amount Required - " & MyApp$ & ", rev. " & MY_VERSION$ 420 Exit Sub 430 End...
8
1558
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 double-pointed structure ? I guess (*p)->item, but I wonder if writing *p->item is the same thing. What's the precedence in this case ? Thanks in advance. -- Emanuele Blanco PGP Key Available
4
1900
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 postfix-expression ( argument-expression-listopt ) postfix-expression . identifier postfix-expression -> identifier postfix-expression ++
4
1412
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
1511
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() { Base * base = Create(object_id) ; *this = *dynamic_cast<MyObject*>(base) ; //<- this line
2
1466
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
8057
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8329
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6010
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2472
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.