473,385 Members | 1,838 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,385 software developers and data experts.

if(a,b,x,y)

can somebody explain the working of the following C code?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.   int a=0, b=20;
  5.   char x=1, y=10;
  6.   if(a,b,x,y)
  7.     printf("bye");
  8.   else
  9.     printf("BYE");
  10. }
Output:
bye
Mar 14 '07 #1
6 9461
sicarie
4,677 Expert Mod 4TB
can somebody explain the working of the following C code?

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.   int a=0, b=20;
  5.   char x=1, y=10;
  6.   if(a,b,x,y)
  7.     printf("bye");
  8.   else
  9.     printf("BYE");
  10. }
Output:
bye
Somebody was coding after hitting the bar?

Whoops, wrong type of explanation. All I can do is venture a guess, so here it is:

C/C++ compilers see anything that isn't 0 as true, so the latter values overrode the 'a' in the 'if' statement, and put non-zero values in there, which the compiler then read as 'true'. But that's just my guess.
Mar 14 '07 #2
Ganon11
3,652 Expert 2GB
It appears that the commas are acting as or statements - in other words, the "if (a,b,x,y)" is operating like "if (a || b || x || y)". Since C/C++ interpret true/false as nonzero and zero respectively, a is the only 'false' value - thus, the if statement evaluates to true and prints "bye".
Mar 14 '07 #3
Ganon11
3,652 Expert 2GB
Nevermind, sicarie is right. The latter variables must be overriding the former, so in if (a, b, x, y) only y is being checked.
Mar 14 '07 #4
Nevermind, sicarie is right. The latter variables must be overriding the former, so in if (a, b, x, y) only y is being checked.
thanku so much.. indeed the last expression overrides all the other expressions in if statement..
Mar 14 '07 #5
Banfa
9,065 Expert Mod 8TB
OK folks the important thing here is that , is an operator (like + - [] == to name a few others). The value of the comma operator is the right hand expression, however you need to be careful because the comma operator has the lowest precedence of all operators so in this code

Expand|Select|Wrap|Line Numbers
  1. int a;
  2. int b = 10;
  3. int c = 20;
  4.  
  5. a = b, c;
  6.  
a ends with a value of 10 (from b) but the value of the entire expression is 20 (from c) because due to operator precedence it is interpreted as

Expand|Select|Wrap|Line Numbers
  1. (a = b), c;
  2.  
Generally speaking I think that unless you are trying to obsfucate your code the comma operator is best avoided.
Mar 14 '07 #6
sicarie
4,677 Expert Mod 4TB
OK folks the important thing here is that , is an operator (like + - [] == to name a few others). The value of the comma operator is the right hand expression, however you need to be careful because the comma operator has the lowest precedence of all operators so in this code

Expand|Select|Wrap|Line Numbers
  1. int a;
  2. int b = 10;
  3. int c = 20;
  4.  
  5. a = b, c;
  6.  
a ends with a value of 10 (from b) but the value of the entire expression is 20 (from c) because due to operator precedence it is interpreted as

Expand|Select|Wrap|Line Numbers
  1. (a = b), c;
  2.  
Generally speaking I think that unless you are trying to obsfucate your code the comma operator is best avoided.
That makes much more sense - thanks Banfa.

(I knew the person who wrote that had been to the bar....)
Mar 14 '07 #7

Sign in to post your reply or Sign up for a free account.

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.