Consider this "EXAM" question:
int main()
{
int a=10,b=20;
char x=1,y=0;
if(a,b,x,y)
{
printf("EXAM");
}
}
Please let me know why this doesn't print anything at all.
Comma operator - evaluates 1st expression and returns the second one. So a,b,x,y will return value stored in y, that is 0.
Others already mentioned that the comma operator returns the right-most value. If you want to get the value printed if ANY of these variables is true use the logical or:
int main()
{
int a=10,b=20;
char x=1,y=0;
if(a || b || x || y)
{
printf("EXAM");
}
}
But then be aware of the fact that the comma evaluates all expressions whereas the or operator stops as soon as a value is true. So with
int a = 1;
int b;
if(a || b = 1) { ... }
b has an undefined value whereas with
int a = 1;
int b;
if(a, b = 1) { ... }
b will be set to 1.
"Printed Nothing"and I don't see a C question being outputted either...