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;
}
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 3 of 3.
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.
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.
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
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
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.
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.
Vineela said:
1 decade ago
m=-2 && 3 && 1;
I am not getting this statement. Please explain me.
I am not getting this statement. Please explain me.
Narayan pandey said:
1 decade ago
Please make me understand.
Mandy said:
1 decade ago
Not able to understand that. Kindly explain again.
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'.
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'.
SanLakshmi said:
1 decade ago
I can't understand the AND operation.. can anyone explain it?..
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers