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 1 of 3.
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.
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.
Jebby said:
7 years ago
main()
{
int i=1,j=1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d%d%d%d%d,,i,j,k,l,m);
}
For this, I'm getting the output of 22131.
In this case, l should not get incremented..It should be 2.I don't know why we are getting 3.
Please, anyone explain.
{
int i=1,j=1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d%d%d%d%d,,i,j,k,l,m);
}
For this, I'm getting the output of 22131.
In this case, l should not get incremented..It should be 2.I don't know why we are getting 3.
Please, anyone explain.
Saurabh said:
1 decade ago
How could you say that if half of the expression is true then there is no need to execute complete expression?
As in above case as left side part is true so its not increment value of k by 1. Can anyone elaborate?
As in above case as left side part is true so its not increment value of k by 1. Can anyone elaborate?
Rose said:
8 years ago
-2&&3 how in and operation the multiplication of false value * true value becomes as false value only know?
Explain to me that how you are assuming -2 as a true value?
Explain to me that how you are assuming -2 as a true value?
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.
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;
Shaikh Sakib said:
5 years ago
When it checks a && b and Find True. So It will again Check The Other condition. if It false than It will not Check conditions So Why There is k=0?
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?
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 ||.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers