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 1 of 3.

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?

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.

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.

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)

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?

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.

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

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

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?

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?


Post your comments here:

Your comments will be displayed after verification.