...

View Full Version : C Question



phantom007
07-15-2008, 03:38 PM
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

oracleguy
07-15-2008, 04:49 PM
That is just the results of logical and and logical or operations. They can either be a 1 or a 0 and then it prints them all out together with that printf.

phantom007
07-15-2008, 04:55 PM
yes but i need to know whats the logic behind it

abduraooft
07-15-2008, 05:09 PM
See http://en.wikipedia.org/wiki/Logical_conjunction#Applications_in_computer_programming
(4 is equivalent to 0100 in binary)

oracleguy
07-15-2008, 10:32 PM
int i=4,j=-1,k=0,w,x,y,z;
w=i||j||k; True OR True OR False = True
x=i&&j&&amp;k; True AND True AND False = False
y=i||j&&amp;k; True AND True AND False = True
z=i&&j||k; True AND True OR False = True


See:
http://en.wikipedia.org/wiki/Logical_conjunction
http://en.wikipedia.org/wiki/Logical_disjunction



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum