What is the output of the below program?
#include<stdio.h>
int main()
{
int a=2,b=4,c=3,d=0,x,y;
x= (a=0) && (b=7);
y=(c=5) || (d=0);
printf("a=%d,b=%d,c=%d,d=%d,x=%d,y=%d\n",a,b,c,d,x,y);
x=(a==0) && (b=10);
y=(c==6) || (d=5);
printf("a=%d,b=%d,c=%d,d=%d,x=%d,y=%d\n", a,b,c,d,x,y);
}
Right Answer.
1) Initially, a=2,b=4,c=3,d=0,x = garbage,y = garbage;
2) x= (a=0) && (b=7);
a = 0 means '0' assigned to 'a' and it is false. As a = 0 gives false, b = 7 will not be executed. Because for logical AND operation, if the first condition is "false" then the result will be "false" always.
x= (a=0) && (b=7) = false = 0
Now, a=0,b=4,c=3,d=0,x = 0,y = garbage.
3) y=(c=5) || (d=0);
c = 5, '5' is assigned to 'c' and the condition will be true. d = 0, '0' is assigned to 'd', and condition will be false.
y=(c=5) || (d=0) = true || false =. true = 1;
Now, a=0,b=4,c=5,d=0,x = 0,y = 1.
First, printf o/p will be: a=0,b=4,c=5,d=0,x=0,y=1
4) x=(a==0) && (b=10) = true && true = true = 1
Now, a=0,b=10,c=5,d=0,x=1,y=1
5) y=(c==6) || (d=5) = false || true = true = 1
Now, a=0,b=10,c=5,d=5,x=1,y=1
For the second printf o/p will be: a=0,b=10,c=5,d=5,x=1,y=1