C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 7)
7.
What will be the output of the program?
#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j || ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
Answer: Option
Explanation:
Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i && ++j || ++k;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;.
(++k) is not executed because (-2 && 3) alone return TRUE.
Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one).
Hence the output is "-2, 3, 0, 1".
Discussion:
21 comments Page 2 of 3.
Omi said:
1 decade ago
++Pre increment is having highest priority than (&& and ||).
Then why k=0?
Then why k=0?
Harry said:
1 decade ago
The precedence rules only say that it will be evaluated like this:
++a || (++b && ++c);
Now comes the short circuiting behavior of the logic operators which says that you have to evaluate terms from left to right and stop when the result is known. The part on the right never gets executed.
Precedence and order of evaluation are two completely different things. For logical operator expressions, evaluation is always left-to-right. The expression ++a || ++b && ++c is interpreted as:
Evaluate ++a
If the result of 1 is zero, evaluate ++b && ++c
The expression is parsed as ++a || (++b && ++c); the whole expression is true if either of the subexpressions ++a or ++b && ++c is true.
++a || (++b && ++c);
Now comes the short circuiting behavior of the logic operators which says that you have to evaluate terms from left to right and stop when the result is known. The part on the right never gets executed.
Precedence and order of evaluation are two completely different things. For logical operator expressions, evaluation is always left-to-right. The expression ++a || ++b && ++c is interpreted as:
Evaluate ++a
If the result of 1 is zero, evaluate ++b && ++c
The expression is parsed as ++a || (++b && ++c); the whole expression is true if either of the subexpressions ++a or ++b && ++c is true.
Uvarekha said:
1 decade ago
The expression involving unary operators are evaluated from right to left? then option C is wrong.
Bhawana said:
1 decade ago
First thing, for pre increment we go from right to left in either way, pre increment has higher priority than logical && or ||.
Sudeepta said:
1 decade ago
We always go from LEFT to RIGHT.So in this case also we start from left. Step by step as soon as we solve the expression from left, we get:
-2 && 3 || ++k
So the expression in the left becomes true. Also in case of || i.e. 'or' if any one expression gets true,the result turns out to be true i.e. 1, hence, eliminating the need to solve ++k.
The result we get is true, therefore m = 1.
-2 && 3 || ++k
So the expression in the left becomes true. Also in case of || i.e. 'or' if any one expression gets true,the result turns out to be true i.e. 1, hence, eliminating the need to solve ++k.
The result we get is true, therefore m = 1.
Navneet said:
1 decade ago
I agree with naveen that pre increment highest priority.
So it will be
-2 && 3 || 1.
So it will be
-2 && 3 || 1.
Kiran said:
1 decade ago
Offcourse.
But here -2 && 3 gets true in the above notations first -2 && 3.
Gets true so it will terminate at at that point thats why k will not increment.
But here -2 && 3 gets true in the above notations first -2 && 3.
Gets true so it will terminate at at that point thats why k will not increment.
Naveen said:
1 decade ago
Pre increment is having highest priority than (&& and ||)
so it should be
-2 && 3 || 1
Please advice why it is not so?
so it should be
-2 && 3 || 1
Please advice why it is not so?
Neeraja said:
1 decade ago
-2 is non zero value and 3 is also non zero value... any non zero value returns true so -2 && -3 becomes true
Vigneswari said:
1 decade ago
i=-3;
j=2;
k=0;
++i && ++j = -2 && 3 = 1 (any one true=true)
1 || ++k = 1 (k will not be incremented because already 1 becomes true)
so ans m=1 and k=0;
j=2;
k=0;
++i && ++j = -2 && 3 = 1 (any one true=true)
1 || ++k = 1 (k will not be incremented because already 1 becomes true)
so ans m=1 and k=0;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers