Examveda
Examveda

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);
}

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 expression 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.

This Question Belongs to C Program >> Operators And Expressions

Join The Discussion

Comments ( 5 )

  1. Abhijeet Kulkarni
    Abhijeet Kulkarni :
    11 months ago

    need video explaination basic

  2. Sandip Majumdar
    Sandip Majumdar :
    3 years ago

    Its very fine

  3. Ajinkya Londhe
    Ajinkya Londhe :
    7 years ago

    its correct ohh yeahh

  4. Examveda
    Examveda :
    7 years ago

    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.

  5. Sonika Saini
    Sonika Saini :
    7 years ago

    why k++ is not increment the value of k

Related Questions on Operators and Expressions