473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

if(a,b,c)

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

Nov 15 '05 #1
7 1580
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: <mailto:Th*************@gmail.com>

Nov 15 '05 #2


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

--
Er*********@sun.com

Nov 15 '05 #3
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.

Nov 15 '05 #4
"hyderabadblues" <si*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Nov 15 '05 #5
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.
Nov 15 '05 #6

"hyderabadblues" <si*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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 !=

Nov 15 '05 #7

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

Nov 15 '05 #8

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

Similar topics

5
by: vishal | last post by:
hi i am using session in php. but if the client has disabled his cookie then will my session work or not????????? thxs for your reply in advance.............
3
by: JD | last post by:
I have the below code that displays a small thumbnail if a user is saluted, if they aren't saluted it checks to see if they might have a profile picture, this all works with the exception that is...
11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
4
by: Ryan Lowe | last post by:
i thought id ask here before wirting a PEP, if people thought it would be a good enhancement to allow if clauses in regular for-statements like so: >>> for x in y if x < 10 : is semantically...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
6
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
17
by: Keith | last post by:
How can I do the following: If recordset is empty, redirect to page1 but if recordset has something in, redirect to page 2. Thanks
6
by: Oplec | last post by:
Hi, I thought that I understood how C++ allows for the declaration and defining of variables within an if() statement and how the declared variable can be used until the end of the major if()...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.