can someone explain me the output of following program

#include <stdio.h>

int main(void)
{
int i=4,j=1,k=0,w,x,y,z;
w=i||j||k;
x=i&&j&&k;
y=i||j&&k;
z=i&&j||k;
printf(“w=%d x=%d y=%d z=%d\n”,w,x,y,z);
return 0;
}

Please use sites like ideone to keep a good format to the code. For now its quite a small code so its readable.

Here, the thing is that in C++, 0 is false and every nonzero value is true. || is an operator that returns true if any of the operands is true and && is an operator that returns false if any one of the operands is false. So. w=i||j||k will set w’s value to be 1(true) because both i and j is true(4 and 1 respectively). Similarly, x will become 0, y will become 1(&& is executed before ||) and z becomes 1. So, w, y and z are 1 and x is 0.

Hope this helps :slight_smile:

what if value of j is -1?

If the value of j=-1, then also you will get same answer. The thing matters here is, value 0. Any non-zero value will be equivalent Boolean 1. So -1 also implies Boolean 1.

//