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 1 of 3.
R.mahesh said:
5 years ago
And operator is like compulsion means if all the conditions are true it will return 1 (true) and if any one condition is false it will return 0 (false). And basically declaration is int (integer) so it ranges from - to +.
(1)
Dhanashri said:
7 years ago
Hi;
AND IS LOGICAL OPERATOR.
It's operation having true value when both the values are true. i.e both value are non zero,
In our example both values are non zero.
m=-2&&3&&1.
Here -2 =non zero number and 3=non zero mean not equal to zero and 1 is also non zero therefore first and operation gives true and finally, we get true i.e m=1.
AND IS LOGICAL OPERATOR.
It's operation having true value when both the values are true. i.e both value are non zero,
In our example both values are non zero.
m=-2&&3&&1.
Here -2 =non zero number and 3=non zero mean not equal to zero and 1 is also non zero therefore first and operation gives true and finally, we get true i.e m=1.
(1)
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.
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)
Siri said:
8 years ago
Can anyone explain -2 && 3 && 1?
(1)
Teju said:
8 years ago
Explain && operator.
Ramkumar said:
3 years ago
Hi, whether m expression will execute from left to right or right to left?
Can anyone have an idea? please describe it.
Can anyone have an idea? please describe it.
Aafrin Shaqthaj said:
5 years ago
Is that the AND operator consider the negave value as True?
Tejanth said:
5 years ago
What would if we have post increment..i.e i++?
Please tell me.
Please tell me.
PIYUSH said:
6 years ago
How they increment the value of I, j, k by 1. Please anyone, explain it.
Srinivas said:
6 years ago
Clear explanation, Thanks @Sachendra Niranjan.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers