C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 1)
1.
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, 3, 1, 1
2, 3, 1, 2
1, 2, 3, 1
3, 3, 1, 2
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 && 1;
becomes m = TRUE && 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,k are increemented by '1'(one).

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

Discussion:
28 comments Page 1 of 3.

SanLakshmi said:   1 decade ago
I can't understand the AND operation.. can anyone explain it?..

Koushik said:   1 decade ago
@above:

In case of step 2:

m= -2 && 3 && 1;

All those are not equal to zero.so those values(-2,3,1) are true. In AND operation if all trues then result would be true.

In 'C', TRUE value can be represented with value '1' and false as '0'.

so it returns '1' value and assigned to variable 'm'.

Mandy said:   1 decade ago
Not able to understand that. Kindly explain again.

Narayan pandey said:   1 decade ago
Please make me understand.

Vineela said:   1 decade ago
m=-2 && 3 && 1;

I am not getting this statement. Please explain me.

Vidya sagar said:   1 decade ago
#include<stdio.h>
int main()
{
int i=-1, j=2, k=0, m;
m = ++i && ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
If we changed the value of i= -1 then output we got
0, 2, 0, 0 (GCC compiler).

Anybody can suggest us how this compiler give the output and
How precedence operator work.

Sachendra niranjan said:   1 decade ago
Hi,
I am trying to explain this question.

Here value assigned to i=-3,j=2,k=0;
In next step m = ++i && ++j && ++k;

Here,we are using pre-increment operator .so first value incremented by one
And now value of i,j and k are -2,3,1 respectively..

Now expressions is -2 && 3 && 1

And we know that minus value stored in memory in its 2's compliment so stored value for -2 is 2's compliment of its binary
11111111 11111110(in 16 bit compiler) and 3 would stored as 00000000 00000011 and 1 would stored as 00000000 00000001 .

Now expression evaluated in this way
m = 11111111 11111110 && 00000000 00000011 && 00000000 00000001

In this expression 11111111 11111110 && 00000000 00000011 will evaluated non zero value. so it becomes TRUE and return 1 .

Now expression is m = 1 && 00000000 00000001
1 && 0000000 00000001 it also evaluated non zero value.so it becomes TRUE and return 1 so this i initialized to m

Hence, output is -2,3,1,1

Gouthami said:   1 decade ago
int i=-1, j=2, k=0, m;
m = ++i && ++j && ++k;

While evaluating the above statement ++i becomes 0 so right hand side operators to logical and operand j and k doesn't gets executed and m value equals to 0.

Murali krishna said:   1 decade ago
Can any one explain how we take binary form for -2.

Lokesh jain said:   1 decade ago
-2 && 3 && 1
It logical operators it checks from left to right.
Here logical operator is AND so first check -2 it is non zero value so it proceed for 3 same non zero value, than proceed for 1 it is non zero again so all are true so answer is 1.

For eg if it is m=-2 && 3 && 0.
For last 0 it is treated as false so (3&&0) give false ie.0 and again (-2&&0) gives zero so m=0.

Eg:
i=-1, j=3, k=-1;
m=++i && ++j && k++;
Then it evaluate i=0 then it doesn't proceed for left expression for j or k.
Output is m=0 i=0 j=3 k=-1.

But,
if i=-2, j=3, k=-1;
m=++i && ++j && k++;
Then output is m=1 i=-1 j=4 k=0.
Here k is post increment so in k++ it have value -1 but at next line it gets value 0.

And if i=-2, j=3, k=-1;
m=++i && ++j && ++k;
Then output is m=0 i=-1 j=4 k=0.
Here k is pre increment so m is 0 because k is false.
(1)


Post your comments here:

Your comments will be displayed after verification.