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;
}
1, 2, 0, 1
-3, 2, 0, 1
-2, 3, 0, 1
2, 3, 1, 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;
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.

Ajay jain said:   8 years ago
Suppose in the place of ++k. if there will be k++ then the result of k++ is false right?

Please let me know and give an explanation.

Haritha said:   10 years ago
Hello and operation both true values then true but we are said one is 1 and another is 0.

How to say value is true please explain?

Akach said:   8 years ago
Answer can be -2, 3, 1, 1 k value will be incremented to 1 since it is pre-incremented. How the value of k remains 0 here?
(2)

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

Premdev said:   9 years ago
How that answer came k value is 0?

It's that ++k means pre increment operator, So k =k+l..0+1=1 k=1.

Uvarekha said:   1 decade ago
The expression involving unary operators are evaluated from right to left? then option C is wrong.

Navneet said:   1 decade ago
I agree with naveen that pre increment highest priority.

So it will be

-2 && 3 || 1.

Omi said:   1 decade ago
++Pre increment is having highest priority than (&& and ||).

Then why k=0?

VENKAT said:   10 years ago
Why we are not incrementing the value of m? Please explain?

Aman said:   9 years ago
How k = 0? Here, k should be got incremented to 1?


Post your comments here:

Your comments will be displayed after verification.