C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#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\n", w, x, y, z);
return 0;
}
Answer: Option
Explanation:
Step 1: int i=4, j=-1, k=0, w, x, y, z; here variable i, j, k, w, x, y, z are declared as an integer type and the variable i, j, k are initialized to 4, -1, 0 respectively.
Step 2: w = i || j || k; becomes w = 4 || -1 || 0;. Hence it returns TRUE. So, w=1
Step 3: x = i && j && k; becomes x = 4 && -1 && 0; Hence it returns FALSE. So, x=0
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
Step 6: printf("%d, %d, %d, %d\n", w, x, y, z); Hence the output is "1, 0, 1, 1".
Discussion:
51 comments Page 1 of 6.
CHANDRAMANI said:
9 years ago
Here,
Step 4 is wrong,as the associativity of Logical AND and Logical OR operator is from left to right. So,
y = i || j &&k; can be given as y = 4 || -1 && 0;
As we know that "Negative" numbers as well as "Positive" numbers are always taken as "1".
And also in Logical AND operator "Both" the conditions should be "True" then only the result will be "1".
And in Logical OR operator if the condition one is "True" then "there is no need to check the 2nd condition" , ,it will give the result "1" only.
Here,
1st step: y = i || j i.e y = 4 || -1 i.e y= 1 || 1
condition true:result "1"
2nd step: y = 1 && k i.e y = 1 && 0;
condition false:result "0" /*must check 2nd condition(both the condition should be true)*/
Thus result is "0".
Step 4 is wrong,as the associativity of Logical AND and Logical OR operator is from left to right. So,
y = i || j &&k; can be given as y = 4 || -1 && 0;
As we know that "Negative" numbers as well as "Positive" numbers are always taken as "1".
And also in Logical AND operator "Both" the conditions should be "True" then only the result will be "1".
And in Logical OR operator if the condition one is "True" then "there is no need to check the 2nd condition" , ,it will give the result "1" only.
Here,
1st step: y = i || j i.e y = 4 || -1 i.e y= 1 || 1
condition true:result "1"
2nd step: y = 1 && k i.e y = 1 && 0;
condition false:result "0" /*must check 2nd condition(both the condition should be true)*/
Thus result is "0".
Anish Kumar said:
9 years ago
Let t -> true f-> false.
Basic rule:
t && t = t , f && f = f , t && f = f, f && t = f.
t || t = t, t || f = t , f || t = t, f || f = f.
&& has higher precedence than ||.
Try this in your compiler, 1 || 0 && 0.
If you will assume && >> || than you will get true.
If you will assume || == && (equal precedence ) than you will get false but real answer is true.
So, && has higher precedence than ||. So, don't keep any doubt in your mind.
Now, the second confusion I will try to clear it . Consider every negative number as true and positive number, try to solve these questions as:
-4 && 5 => answer is true,
-4 && -5 => answer is true,
4 && 5 => answer is true,
4 && 0 => answer is false,
4 || 5 => answer is true,
-4 || -5 => answe is true,
-4 || 5 => answer is true,
-4 || 0 => answer is true,
0 || 0 => answer is false.
Best Of Luck!
Basic rule:
t && t = t , f && f = f , t && f = f, f && t = f.
t || t = t, t || f = t , f || t = t, f || f = f.
&& has higher precedence than ||.
Try this in your compiler, 1 || 0 && 0.
If you will assume && >> || than you will get true.
If you will assume || == && (equal precedence ) than you will get false but real answer is true.
So, && has higher precedence than ||. So, don't keep any doubt in your mind.
Now, the second confusion I will try to clear it . Consider every negative number as true and positive number, try to solve these questions as:
-4 && 5 => answer is true,
-4 && -5 => answer is true,
4 && 5 => answer is true,
4 && 0 => answer is false,
4 || 5 => answer is true,
-4 || -5 => answe is true,
-4 || 5 => answer is true,
-4 || 0 => answer is true,
0 || 0 => answer is false.
Best Of Luck!
Pooja said:
1 decade ago
@ Wikiok and Richa
Can you please explain me that if the precedence of && operator is more than ||operator then in the explanation of question no 3(previous page) it was written that
"Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1."
From this we can understand that it is evaluated from left.
Can anyone clear my doubt please please??
Can you please explain me that if the precedence of && operator is more than ||operator then in the explanation of question no 3(previous page) it was written that
"Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1."
From this we can understand that it is evaluated from left.
Can anyone clear my doubt please please??
Mukesh Modi said:
1 decade ago
Everyone have doubt in step 4 and step 5.
Here I am clearing your doubt if I am not wrong.
Because of precedence order && got 11 number and || 12 and their associativity is L to R.
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1.
when I compiled -1 && 0 it returns T. so i|| T then answer is T.
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
i&&j that is 4&&-1 returns T. so T || k then answer is T.
so I think any -ve number(-1) is treated as T.
Here I am clearing your doubt if I am not wrong.
Because of precedence order && got 11 number and || 12 and their associativity is L to R.
Step 4: y = i || j &&k; becomes y = 4 || -1 && 0; Hence it returns TRUE. So, y=1.
when I compiled -1 && 0 it returns T. so i|| T then answer is T.
Step 5: z = i && j || k; becomes z = 4 && -1 || 0; Hence it returns TRUE. So, z=1.
i&&j that is 4&&-1 returns T. so T || k then answer is T.
so I think any -ve number(-1) is treated as T.
Sunitha said:
1 decade ago
@Purnima once check out your logic friend.except zero all the numbers are true.
And friend @Vicky kumar here the logical operators have left to right priority but When more than one logical operator is used in a statement, NOT is evaluated first, then AND, and finally OR)
i=4, j=-1, k=0
Here in statement z = i && j || k
= 4 && -1|| 0
= T && T || F
= T
= T || F
= T
AND FINALLY RETURNS 1. AS 1 INDICATES TRUE
And friend @Vicky kumar here the logical operators have left to right priority but When more than one logical operator is used in a statement, NOT is evaluated first, then AND, and finally OR)
i=4, j=-1, k=0
Here in statement z = i && j || k
= 4 && -1|| 0
= T && T || F
= T
= T || F
= T
AND FINALLY RETURNS 1. AS 1 INDICATES TRUE
Sahana S Shenoy said:
7 years ago
All the expressions evaluates from left to right(in case of RHS and in case of w=expression, it is right to left that means expression is evaluated first then assigned to w) as the AND has the highest precedence over ||.
--->the number other than "zero" is always "true"
so w=T||T||F----->T||F
w=T
x=T&&T&&F---->T&&F
x=F
y=T||T&&F----->T||F
y=T
z=T&&T||F------>T||F
z=T.
--->the number other than "zero" is always "true"
so w=T||T||F----->T||F
w=T
x=T&&T&&F---->T&&F
x=F
y=T||T&&F----->T||F
y=T
z=T&&T||F------>T||F
z=T.
(4)
Dipak said:
8 years ago
Binary Number is a combination of 0 and 1.
So in above example, positive(+) numbers are assumed as 1 and negative(-) numbers are assumed as 0.
We all know ANDing operation of two binary digit i.e.
1 1
1 0
-------------
1 0
Note: ANDing means multiplication of binary numbers.
we all know ORing operation of two binary digits i.e.
1 1
1 0
---------------
1 1
Note: ORing means addition of binary numbers.
So in above example, positive(+) numbers are assumed as 1 and negative(-) numbers are assumed as 0.
We all know ANDing operation of two binary digit i.e.
1 1
1 0
-------------
1 0
Note: ANDing means multiplication of binary numbers.
we all know ORing operation of two binary digits i.e.
1 1
1 0
---------------
1 1
Note: ORing means addition of binary numbers.
Anomie said:
5 years ago
@All.
Kindly read below logical operators with examples
&& Logical AND. True only if all operands are true.
If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one operand is true.
If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the operand is 0
If c = 5 then, expression !(c==5) equals to 0.
Kindly read below logical operators with examples
&& Logical AND. True only if all operands are true.
If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one operand is true.
If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the operand is 0
If c = 5 then, expression !(c==5) equals to 0.
(1)
Ahenkan Boakye-Ansah said:
1 decade ago
Step 5: Note this is not Bitwise Or and And. This is Logical OR and AND.
So if z = I && j || k; becomes z = 4 && -1 || 0; z = (1 && 1) || (0); z = (T && T) || (F).
z=1;
Or 4 and -1 are not 0, so it makes the statement true.
Or is 4=0? No.
Or is -1=0? No.
Or is 0=0? Yes.
So, z = (T && T) || (F).
z=1.
So if z = I && j || k; becomes z = 4 && -1 || 0; z = (1 && 1) || (0); z = (T && T) || (F).
z=1;
Or 4 and -1 are not 0, so it makes the statement true.
Or is 4=0? No.
Or is -1=0? No.
Or is 0=0? Yes.
So, z = (T && T) || (F).
z=1.
Khalidzazai said:
9 years ago
Solve it in this manner.
Solve it step by step, please with an explanation.
For eg:
int i=4, j=-1, k=0, w, x, y, z;
y = I || j &&k;
z = I && j || k;
Sol: y = I || (j && k);
y = 4 || (-1 && 0);
y = 4 || (0) ; // gives 0 when -1 && 0.
y = 4; //gives true (1) when 4 or 0
Solve it step by step, please with an explanation.
For eg:
int i=4, j=-1, k=0, w, x, y, z;
y = I || j &&k;
z = I && j || k;
Sol: y = I || (j && k);
y = 4 || (-1 && 0);
y = 4 || (0) ; // gives 0 when -1 && 0.
y = 4; //gives true (1) when 4 or 0
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers