Click to See Complete Forum and Search --> : C Question


cancer10
07-15-2008, 10:38 AM
Why is the following C code outputting 1011? Whats the logic?

#include<stdio.h>
int main()
{
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("%d%d%d%d",w,x,y,z);
getch();
}



Thanx

chazzy
07-15-2008, 11:31 AM
w = i or j or k (4 or -1 or 0), true/1
x = i and j and k (4 and -1 and 0), false/0
y = i or j and k (4 or -1 and 0), true/1
z = i and j or k (4 and -1 or 0), true/1

it's boolean logic translated in numbers.

cancer10
07-15-2008, 12:09 PM
Why is x false?

chazzy
07-15-2008, 04:01 PM
0 is considered false, all other values are true.

so when you do 4 && -1 && 0, you get true && true && false which is false.