Velocity Reviews > if(a,b,c)

if(a,b,c)

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 !=

Kenneth Brody
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 !=

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)>

Eric Sosman
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 !=

Search your C textbook for "the comma operator."

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

Martin Ambuhl
Guest
Posts: n/a

 10-11-2005
> 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.
>

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

Default User
Guest
Posts: n/a

 10-11-2005

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

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

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 !=
>

g.kanaka.raju@gmail.com
Guest
Posts: n/a

 10-12-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 !=

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