Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > if(a,b,c)

Reply
Thread Tools

if(a,b,c)

 
 
hyderabadblues
Guest
Posts: n/a
 
      10-11-2005
Why the following statement evaluates as false infact it doesn't follow
the syntax

int a=10,b=8,c=0;

if(a,b,c)
printf("=");
else
printf("!=");

prints !=

 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      10-11-2005
hyderabadblues wrote:
>
> Why the following statement evaluates as false infact it doesn't follow
> the syntax
>
> int a=10,b=8,c=0;
>
> if(a,b,c)
> printf("=");
> else
> printf("!=");
>
> prints !=


Because c==0, therefore (a,b,c)==0, therefore "if (a,b,c)" is false.

And what syntax does it not follow?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-11-2005


hyderabadblues wrote On 10/11/05 13:54,:
> Why the following statement evaluates as false infact it doesn't follow
> the syntax
>
> int a=10,b=8,c=0;
>
> if(a,b,c)
> printf("=");
> else
> printf("!=");
>
> prints !=


Search your C textbook for "the comma operator."

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-11-2005
hyderabadblues wrote:
> Why the following statement evaluates as false infact it doesn't follow
> the syntax


What do you mean "it doesn't follow the syntax"?
>
> int a=10,b=8,c=0;
>
> if(a,b,c)


The commas are comma operators (what did you think they were?), so the
above is just
if (c)
with the side effect of evaluating a and b along the way.
Since (c == 0), the condition is never true, so ...

> printf("=");
> else
> printf("!=");
>
> prints !=


of course.

And next time please post compilable code.
>

 
Reply With Quote
 
Charles M. Reinke
Guest
Posts: n/a
 
      10-11-2005
"hyderabadblues" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Why the following statement evaluates as false infact it doesn't follow
> the syntax
>
> int a=10,b=8,c=0;
>
> if(a,b,c)
> printf("=");
> else
> printf("!=");
>
> prints !=
>


IIRC, the comma operator results in the evaluation of all arguments, then
returns the value of the LAST (i.e. rightmost) argument. In this case, it
evaluates the expressions "a", "b", and "c" and then returns the value of
the last expression ("c"), which is 0. Thus, the correct behavior is for
the "if" statement to be false, and the "else" block to be executed, as you
observed. What were you expecting it to do?

-Charles


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      10-11-2005
hyderabadblues wrote:

> Why the following statement evaluates as false infact it doesn't
> follow the syntax


Yes it does.

> int a=10,b=8,c=0;
>
> if(a,b,c)


Look in your book to see how the comma operator works.



Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-11-2005

"hyderabadblues" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Why the following statement evaluates as false


Because 'c' evaluates to zero, and zero always
evaluates to false (non-zero always evaluates to
true).

> infact it doesn't follow
> the syntax


Sure it does. It's valid C syntax anyway.
You just don't appear to understand what
that syntax does.

Get a C book (or more).

-Mike

>
> int a=10,b=8,c=0;
>
> if(a,b,c)
> printf("=");
> else
> printf("!=");
>
> prints !=
>



 
Reply With Quote
 
g.kanaka.raju@gmail.com
Guest
Posts: n/a
 
      10-12-2005

hyderabadblues wrote:
> Why the following statement evaluates as false infact it doesn't follow
> the syntax
>
> int a=10,b=8,c=0;
>
> if(a,b,c)
> printf("=");
> else
> printf("!=");
>
> prints !=


The syntax is fine and the behaviour of the program as per the
semantics of "," operator. It's associativity is left-to-right and if a
pair of expressions seperated by a comma is evaluated left-to-right.
So, there's no ambiguity even with the behaviour as well. So, the
program always prints !=.

Regards,
Raju

 
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




Advertisments