Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<conio.h>
#include<stdio.h>
using namespace std;
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("w=%d x=%d y=%d z=%d\n",w,x,y,z);
	getch();
	return 0;
}


What I have tried:

I have got the output but i can't understand that ....PLzz provide me some explanation
Posted
Updated 9-Mar-18 22:30pm
Comments
Richard MacCutchan 10-Mar-18 4:17am    
Read your course notes, or get a copy of a C reference guide, which explains all the logical operators. Also learn to use spacing in your code to make it readable.

Sorry, but it's your homework, not ours - so you will have to do the work here.

Start by remembering that C does not have a "true" or "false" - it considers any number that is not zero to be "true", and any number that is zero to be "false".
Then remember that || and && are logical operators, not binary: so the output of a || b or a && b with be "true" (non-zero) or "false" (zero).
Finally, look up what logical AND and OR operators do: it pretty simple, and the results become obvious.
 
Share this answer
 
Comments
Member 13593396 10-Mar-18 4:01am    
sir i got the output as w=1 x=0 y=1 z=2 but i cannot understand why????
OriginalGriff 10-Mar-18 4:09am    
a) No, you didn't. Look again.
b) Which bit of what you did get did you not understand?
First let's write it as a proper C program (as C++ is not C):
C
#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("w=%d x=%d y=%d z=%d\n", w, x, y, z);
  getchar();
  return 0;
}


Then, consider
C
w = i || j || k = (4) || (-1) || (0) = true AND true AND true = true = 1;

where the expressions with 'true', 'AND', etc. are 'imaginary', just there to illustrate how evaluation happens.
C
x = i && j && k = .. = true AND true AND false = false = 0;

C
y = i || j && k = .. = true OR (true AND false) = true OR false = true = 1;

C
z = i && j || k = .. = (true AND true) OR false = true OR false = true = 1;


in the latest two expressions you see the C operator precedence rules[^] in action (the precedence of && operator is higher than the precedence of the || operator).
 
Share this answer
 
v2
Comments
Member 13593396 17-Mar-18 6:50am    
Thank uu sir for helping me.......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900