please explain this..code

,

void main()

{
int i=2,j=4;
char x=1 ,y=10;
if(y,x,j,i)
printf(“welcome”);

}

SO MANY BUGS…

$g++ main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp:2:11: error: ‘::main’ must return ‘int’
void main()
^
main.cpp: In function ‘int main()’:
main.cpp:4:60: error: ‘printf’ was not declared in this scope
{ int i=2,j=4; char x=1 ,y=10; if(y,x,j,i) printf(“welcome”);
^

NOW IT Compiles and runs…

#include<stdio.h>
int main()

{ int i=2,j=4; char x=1 ,y=10; if(y,x,j,i) printf(“welcome”);
return 0;

}

comma is just changed to && (the logical and operator)!! and then the expression is evaluated…
TRY THIS :

#include<stdio.h>

bool f1()
{
return true;
}

bool f2()
{
return true;
}
int main()

{

if(f1(),f2()) printf("welcome");

return 0;

}

You have the comma operator in action, there.

exp1, exp2, exp3 means exp1 will be evaluated, then exp2 and then exp3. The value of the whole expression id the value of exp3 (the right-most expression).

So, if(y,x,j,i) means if(y!=0, x!=0, j!=0, i!=0) which is again if(true, true, true, true) which boils down to if(true).

So, your if part will get executed.

The final value of the expression representing the comma operator only depends entirely on the right-most expression. It has no connection with the previous expressions. It doesn’t behave like an AND nor does it behave like an OR.

See http://en.wikipedia.org/wiki/Comma_operator and the topic under the heading Comma operator ( , ) in http://www.cplusplus.com/doc/tutorial/operators/ for more details.

4 Likes

this is the 6000th answer,sorry but i am celebrating this one

this is the 6000th answer,sorry but i am celebrating this one

you didn’t even test with bool f2() { return false; }

Congratulations, dude!

But, with your celebration, if there is no use to the community, then what is the point in your celebration over here?

He wanted to get negative karma for sure :smiley: