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 2 of 6.
Mahi said:
8 years ago
How is possible the value of w=1?
Pavithra said:
8 years ago
Please explain the step 2.
Anonymous said:
8 years ago
Anybody please clarify whether we consider associativity or precedence of && and || operators in such case?
Sathish said:
8 years ago
Anybody please clarify the doubt that how would the step 4 gives 1?
Rose said:
8 years ago
I want to know clearly that -1 is assumed as true or false value at the same time how 0 is assumed as true or false value?
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".
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
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!
Smruti ganvir said:
9 years ago
Make me understand how is 4 && -1 && 0 = 0?
Priyanka Priya said:
9 years ago
Can anyone please explain the step 4?
How will it become true?
How will it become true?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers