By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 964 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

if(a,b,x,y)

P: 7
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
Share this Question
Share on Google+
6 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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
Expert 2.5K+
P: 3,652
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
Expert 2.5K+
P: 3,652
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

P: 7
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
Expert Mod 5K+
P: 8,916
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
Expert Mod 2.5K+
P: 4,677
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

Post your reply

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