?
Determine output:
Determine output:
void main()
{
int i=0, j=1, k=2, m;
m = i++ || j++ || k++;
printf("%d %d %d %d", m, i, j, k);
}
void main()
{
int i=0, j=1, k=2, m;
m = i++ || j++ || k++;
printf("%d %d %d %d", m, i, j, k);
}
Answer & Solution
Correct Answer:
Option
B
In this C program, the expression
- It evaluates from left to right.
- It stops evaluating as soon as it finds a true condition because in a logical OR operation, if one operand is true, the result is true.
Let's break it down step by step:
-
-
-
Now, the expression is evaluated:
-
- Since the result is true, the evaluation stops.
After the evaluation:
-
-
-
-
Therefore, the output of the
i++ || j++ || k++ involves the logical OR (||) operator. Here's how it works:
- It evaluates from left to right.
- It stops evaluating as soon as it finds a true condition because in a logical OR operation, if one operand is true, the result is true.
Let's break it down step by step:
-
i is initially 0. i++ returns 0 (post-increment), but it increments i to 1.-
j is initially 1. j++ returns 1 (post-increment), and it increments j to 2.-
k is initially 2. k++ returns 2 (post-increment), and it increments k to 3.Now, the expression is evaluated:
-
0 || 1 is true because one of the operands is true.- Since the result is true, the evaluation stops.
After the evaluation:
-
m is assigned the value 1 because the result is true.-
i is 1 because i was incremented during the evaluation.-
j is 2 because j was incremented during the evaluation.-
k is 2 because k was not incremented further.Therefore, the output of the
printf statement is 1 1 2 2, which corresponds to Option B.
Join the Discussion
Login to post a comment or share your explanation.
LoginIf you read the solution properly you will get to know why 'k' is still remain 2.
When ever Operator || found in expression it work from left to right and stopped at non zero value.