if(a,b,c)

H

hyderabadblues

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

Kenneth Brody

hyderabadblues said:
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: <mailto:[email protected]>
 
E

Eric Sosman

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."
 
M

Martin Ambuhl

hyderabadblues said:
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.
 
C

Charles M. Reinke

hyderabadblues said:
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
 
M

Mike Wahler

hyderabadblues said:
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
 
G

g.kanaka.raju

hyderabadblues said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top