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.
Asim vaniya said:
7 years ago
int i=-3,j=2,k=0,m;
m = ++i && ++j || ++k;
printf("\n i=%d j=%d k=%d m=%d",i,j,k,m);
What is the output of this one?
m = ++i && ++j || ++k;
printf("\n i=%d j=%d k=%d m=%d",i,j,k,m);
What is the output of this one?
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)
Srinivas said:
6 years ago
Clear explanation, Thanks @Sachendra Niranjan.
PIYUSH said:
6 years ago
How they increment the value of I, j, k by 1. Please anyone, explain it.
Tejanth said:
5 years ago
What would if we have post increment..i.e i++?
Please tell me.
Please tell me.
Aafrin Shaqthaj said:
5 years ago
Is that the AND operator consider the negave value as True?
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)
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers