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);
}
A. 1 1 2 3
B. 1 1 2 2
C. 0 1 2 2
D. 0 1 2 3
E. None of these
Answer: Option B
Solution(By Examveda Team)
In this C program, the expressioni++ || 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
Comments ( 5 )
Related Questions on Operators and Expressions
What does the ++ operator do in C when applied to a variable?
A. Decrements by 1
B. Adds 1
C. Doubles the value
D. Leaves it unchanged
need video explaination basic
Its very fine
its correct ohh yeahh
Hello Sonika,
If 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.
why k++ is not increment the value of k