Sitemap
Minhazul Abedin Munna

Minhazul Abedin Munna

Software Engineer | Love to learn new Technology

What will be output of following code?

3 min readNov 3, 2024

--

Question 01:

#include <stdio.h>
int main()
{
int i = 0;
for(i = 0; i < 20; i++) {
switch(i) {
case 0: i += 5;
case 1: i += 2;
case 5: i += 5;
default: i += 4;
break;
}
}
printf("%d", i);
}

Let’s analyze how the code works step-by-step.

The initial setup:

  • i starts at 0.
  • The loop runs for(i = 0; i < 20; i++).

Within each iteration of the for loop:

  • The switch statement executes, checking the current value of i.
  • The cases inside the switch don't use break statements until default, so the cases "fall through," meaning once a case is matched, subsequent cases are also executed until a break or the end of the switch.

Let’s go through each iteration in detail:

Step-by-Step Execution

Iteration 1: i = 0

  • Switch starts with case 0 because i = 0.
  • i += 5 (so i becomes 5).
  • Falls through to case 1, so i += 2 (making i = 7).
  • Falls through to case 5, so i += 5 (making i = 12).
  • Falls through to default, so i += 4 (making i = 16).
  • Break statement in default exits the switch.

Iteration 2: i = 17 (incremented by for loop)

Switch starts with default (since i does not match any case).

  • i += 4 (making i = 21).
  • Break statement in default exits the switch.

Loop ends: Now i = 22, which is greater than 20, so the loop stops.

The final value of i is 22, so the program prints 22.

Question 02:

Get Minhazul Abedin Munna’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s go through the code step-by-step to understand why it outputs 1.

#include <stdio.h>
int main()
{
int i = 2, j = 2;
while(i + 1 ? --i : j++) {
printf("%d", i);
}
}

Explanation of the Code

  1. Variable Initialization: i and j are both initialized to 2.
  2. While Loop Condition: i + 1 ? --i : j++
  • The loop condition uses the ternary operator (condition ? expr_if_true : expr_if_false).
  • The expression i + 1 is evaluated first:
  • Since i = 2, i + 1 becomes 3, which is non-zero (truthy).
  • Because of this, the true branch (--i) is executed instead of j++.
  • The while loop will continue as long as the result of the ternary operator is non-zero.
  1. Loop Execution:
  • Let’s go through each iteration of the loop to see how i changes.

Step-by-Step Execution

  1. First Iteration:
  • i = 2
  • i + 1 is 3, which is truthy, so --i is executed.
  • i becomes 1 (--i decrements i by 1).
  • printf("%d", i); prints 1.

2. Second Iteration:

  • i = 1
  • i + 1 is 2, which is still truthy, so --i is executed again.
  • i becomes 0.
  • Since printf("%d", i); is inside the loop body, it does not execute in this iteration because the loop condition evaluates to 0 (falsy) after the decrement.

3. Loop Exit:

  • The loop exits because i + 1 becomes 0 when i is 0, which makes the ternary condition false.
  • No further values are printed.

The only value printed by printf is 1.

Question 03:

#include <stdio.h>
int main()
{
int i, j;
i = j = 2, 3;
while (--i && j++) {
printf("%d - %d", i, j);
}
}

Explanation of the Code

Variable Initialization:

  • i = j = 2, 3;
  • This line might seem confusing because of the comma.
  • i = j = 2 is executed first due to operator precedence, which sets both i and j to 2.
  • The , 3 part is simply ignored because it has no effect here; it's an isolated expression after the assignments.

So, i and j are both initialized to 2.

While Loop:

  • while (--i && j++)
  • The loop condition has two expressions: --i and j++.
  • The && (logical AND) operator means the loop will continue as long as both --i and j++ evaluate to non-zero (true).

Let's go through each iteration of the loop.

Step-by-Step Execution

First Iteration:

  • --i: i is decremented from 2 to 1.
  • j++: j is currently 2, but after j++, it increments to 3.
  • Since --i is 1 (true) and j++ was 2 (also true), the loop condition is satisfied, and the loop body executes.
  • printf("%d - %d", i, j); prints the current values of i and j, which are 1 and 3, respectively.

Second Iteration:

--i: i is decremented from 1 to 0.

Since --i is now 0 (false), the loop condition --i && j++ fails, and the loop exits without printing anything further.

Output: 1 – 3

--

--

Minhazul Abedin Munna
Minhazul Abedin Munna