C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 3)
3.
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;
}
2, 2, 0, 1
1, 2, 1, 0
-2, 2, 0, 0
-2, 2, 0, 1
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; 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.

Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of variable 'i' only increemented by '1'(one). The variable j,k are not increemented.

Hence the output is "-2, 2, 0, 1".

Discussion:
41 comments Page 2 of 5.

Janani said:   7 years ago
The priority wise we decide only which belongs to which operator.. but the operation for short circuit operands (||,&&)are always left to right.

See, here the question is;

m=++i||b++&&++k
step 1: the compiler will decide which is belongs to which operand according to the priority.
So, the above statement will become like.

m= ++i || (b++ && ++k);
step 2: even after assigning like this operation is always from left to right.
so ++i is -2 i,e TRUE .
so,
m=TRUE|| (++j&&++k);
now it won't check the rest.
because it is short-circuited operator. if 1st argument is TRUE it won't check 2nd argument. because;
for ||
TRUE || TRUE=TRUE
TRUE || FALSE=TRUE
FALSE || TRUE = TRUE
FALSE || FALSE= FALSE
So it will check the second argument only if 1st argument is false.
But for our problem 1st arg is true.. so it won't check the rest.

Note: You may have another question like why j and k values are not incremented.
if we do like that then we are violating the short circuit operator rule.
(1)

Deepak Singh said:   7 years ago
It is -2, 3, 0, 1.

Srikanth said:   7 years ago
I think operator precedence of logical AND (&&) is greater than logical OR (||).


First, we have to do logical AND operation and then logical OR operation.

Dharani said:   8 years ago
In this question for OR operator if anyone condition is true output is true so according to this we don't want to evaluate other operators so time for doing this operation is saved and speed if this process increases. This is compiler optimisation.

Sach said:   8 years ago
According to operator precedence increment operator evaluates first and the logical operators.

So, why values of j and k not incremented?

Navaneethan said:   9 years ago
The answer is -2,3,1,1.

Because of ++i,++j.,++k.

Prakash said:   9 years ago
int i=-1, j=-1, k=1, m;
m = ++k && ++j || ++i;
printf("%d, %d, %d, %d\n", i, j, k, m);

In this case, the whole expression is evaluating the result is 0, 0, 2, 0 bit in the example problem it is evaluating only left.

Electronics said:   9 years ago
&& and || operator guarantees left to right EVALUATION.

For example : E1 && E2 (E1 and E2 are subexpressions) then always E1 will evaluate before E2. Now if E1=0 no need to evaluate E2 as a result of the expression will be zero if E1=0 and if E1 is not equal to 0 then E2 will be evaluated.

In this question m = ++i || ++j && ++k; even if we increment j and k first we are actually violating left to right evaluation rule for || operator as m= E1 || E2, where E1= ++i, E2= ++j && ++k;

According to rule E1 should evaluate before E2 so we can't increment j and K and also we can't evaluate && after increment.

Therefore ++i operates first and since i=logical 1 no need to evaluate E2 (which is ++j && ++k).

Rahul said:   10 years ago
What is output of following?

1) ++a+a+++--a.

2) ++a+--a.

Nagaraju Thota said:   10 years ago
A correctly formed expression that may produce an unexpected result.

Means here m = ++i || ++j && ++k;

Then Formed Expression is m = ++i || (++j && ++k); This is called Grouping the expression.

Then It starts evaluating from left to right also. When evaluating the expression in assignment operation ++i becomes non-zero. So it takes it as true and not evaluates the rest of the expression.

Take the reverse case also that is m= (++j && ++k) || ++i;

Then ++i is not get evaluated as based upon left to right evaluation.

So the conclusion is that there is preference for left to right evaluation as well after grouping the expression.


Post your comments here:

Your comments will be displayed after verification.