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 4 of 6.
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.
Pradip walghude said:
1 decade ago
&& and || are same priority precedence. It execute right to left.
Udaya said:
1 decade ago
I think the Logic is like whenever they got OR operator they are not going for further expression as OR operator will return 1 of one operand is 1. And if the operator is && they were executing the whole expression.
Raji said:
1 decade ago
Please explain about the precedence of && and || operator and also explain the how the second and third step is executed.
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
Purnima said:
1 decade ago
Step 1: i = 4, j = -1, k = 0
Take all values which are greater than or equal to zero as TRUE(T) and which are less than zero as FALSE(F).
Step 2: w = i || j || k
= 4 || -1 || 0
= (T || F) || T
= T || T = T
w = 1
Similarly we get all the expression. Check it out.
Take all values which are greater than or equal to zero as TRUE(T) and which are less than zero as FALSE(F).
Step 2: w = i || j || k
= 4 || -1 || 0
= (T || F) || T
= T || T = T
w = 1
Similarly we get all the expression. Check it out.
Geetha said:
1 decade ago
In step 4 how that answer will come. Please give me explanation.
Vicky kumar said:
1 decade ago
In step 4 the answer should be false but Wikiok said the right answer is true. It can't be happen because logical 'and' or 'or'.
Operator have left to right precedency.
But you are saying right to left how it is possible.
Please give me details.
Operator have left to right precedency.
But you are saying right to left how it is possible.
Please give me details.
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??
Monika said:
1 decade ago
Can anyone please tell the operation of && and ! operators.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers