Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sequential-evaluation operation result

Reply
Thread Tools

sequential-evaluation operation result

 
 
Shivanand Kadwadkar
Guest
Posts: n/a
 
      01-01-2011
I far i know sequential-evaluation operator are evaluated from left
and right and it will only take the right most result

#include<stdio.h>
int main()
{
int i=0,j=2,k=5;
int ans1,ans2;

ans1=(i,j,k); // Evaluated from left to right and we assign 5 to ans1
ans2=i,j,k; // Why it is not same here that we assign 5 to ans2
printf("ans1 %d ans2 %d \n",ans1,ans2);

return 0
}
Output:
ans1 5 ans2 0
-------------------------------------------------

My doubt is with ans2, is comma treated as separator here and we
discard the right value and take only the left one
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      01-01-2011
On 2011-01-01, Shivanand Kadwadkar <(E-Mail Removed)> wrote:
> int i=0,j=2,k=5;
> int ans1,ans2;
>
> ans1=(i,j,k); // Evaluated from left to right and we assign 5 to ans1
> ans2=i,j,k; // Why it is not same here that we assign 5 to ans2


``='' binds stronger than ``,'' so the last expression is parsed as

(ans2=i),j,k;
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      01-01-2011
On 2011-01-01, Shivanand Kadwadkar <(E-Mail Removed)> wrote:
> I far i know sequential-evaluation operator are evaluated from left
> and right and it will only take the right most result


No, you don't. You just said something totally incoherent.

> ans1=(i,j,k); // Evaluated from left to right and we assign 5 to ans1


There is no left-to-right or right-to-left.

Evaluation is not the same thing as *grouping*. What is relevant here
is that you have an assignment of
ans1
=
(i,j,k)

> ans2=i,j,k; // Why it is not same here that we assign 5 to ans2


Because here, you have
ans1=i
j
k

Which is to say, commas have lower precedence than equals signs; the
expressions separated by commas can be assignment expressions including
equals signs, so instead of assigning the value "i,j,k" to ans1, you're
assigning the value "i" for your first expression, then j, then k.

What you may be missing is that in C, assignment is just another kind
of expression, not a "statement" or something.

> My doubt is with ans2, is comma treated as separator here and we
> discard the right value and take only the left one


The word "doubt" is not correct here in standard English. A "doubt"
is where someone told you something and you don't believe them.

Anyway, C generally doesn't specify order of evaluation. Comma operators
are special in that they do, but "order of evaluation" is totally
distinct from "grouping".

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boolean operation and arithmetic operation Buzz Lightyear C++ 10 08-12-2009 01:27 PM
Unpredictable result of Increment operation bintom C++ 9 05-10-2008 03:37 PM
I/O operation, file operation behaviou raan C++ 2 08-16-2007 07:13 PM
Does bit operation always work more efficiently than math operation? david ullua C Programming 13 03-01-2006 11:02 PM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM



Advertisments