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.
Rohit Maru said:
1 decade ago
Priority of logical and (&&) is more than logical or (||).
Rohit said:
1 decade ago
Is there any priority in solving?
Bharadwaj said:
1 decade ago
In step 4, how the y value become true. i.e 4 || -1 && 0. How it results true.
4 || -1 is true and true && 0 means false. But it is returning true. Why?
Plz clarify my doubt
4 || -1 is true and true && 0 means false. But it is returning true. Why?
Plz clarify my doubt
Wikiok said:
1 decade ago
As Rohit Maru said: && has higher priority than ||. So left-to-right associativity can not be used.
Step 4:
1st substep: "-1 && 0" --> False
2nd substep: "4 || False" --> True
So the answer is True, so it is "1".
Step 4:
1st substep: "-1 && 0" --> False
2nd substep: "4 || False" --> True
So the answer is True, so it is "1".
Pallavi said:
1 decade ago
Can anyone explain step3 and step5 please ?
Richa goel said:
1 decade ago
Since the peccedence of && operator is more than the preccedence of || operator
so, (j&&k) is solved first in step 4 .
hence the ans true.
and similarly in step 5.
so, (j&&k) is solved first in step 4 .
hence the ans true.
and similarly in step 5.
Richa said:
1 decade ago
Please explain the precedence and associativity of these operator.
Monika said:
1 decade ago
Can anyone please tell the operation of && and ! operators.
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??
Sahithi said:
2 decades ago
Please give clear explanation.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers