What will be output of following code?
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:
istarts at0.- The loop runs
for(i = 0; i < 20; i++).
Within each iteration of the for loop:
- The
switchstatement executes, checking the current value ofi. - The cases inside the
switchdon't usebreakstatements untildefault, so the cases "fall through," meaning once a case is matched, subsequent cases are also executed until abreakor the end of theswitch.
Let’s go through each iteration in detail:
Step-by-Step Execution
Iteration 1: i = 0
- Switch starts with
case 0becausei = 0. i += 5(soibecomes5).- Falls through to
case 1, soi += 2(makingi = 7). - Falls through to
case 5, soi += 5(makingi = 12). - Falls through to
default, soi += 4(makingi = 16). - Break statement in
defaultexits theswitch.
Iteration 2: i = 17 (incremented by for loop)
Switch starts with default (since i does not match any case).
i += 4(makingi = 21).- Break statement in
defaultexits theswitch.
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
- Variable Initialization:
iandjare both initialized to2. - While Loop Condition:
i + 1 ? --i : j++
- The loop condition uses the ternary operator
(condition ? expr_if_true : expr_if_false). - The expression
i + 1is evaluated first: - Since
i = 2,i + 1becomes3, which is non-zero (truthy). - Because of this, the true branch (
--i) is executed instead ofj++. - The
whileloop will continue as long as the result of the ternary operator is non-zero.
- Loop Execution:
- Let’s go through each iteration of the loop to see how
ichanges.
Step-by-Step Execution
- First Iteration:
i = 2i + 1is3, which is truthy, so--iis executed.ibecomes1(--idecrementsiby 1).printf("%d", i);prints1.
2. Second Iteration:
i = 1i + 1is2, which is still truthy, so--iis executed again.ibecomes0.- Since
printf("%d", i);is inside the loop body, it does not execute in this iteration because the loop condition evaluates to0(falsy) after the decrement.
3. Loop Exit:
- The loop exits because
i + 1becomes0wheniis0, 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 = 2is executed first due to operator precedence, which sets bothiandjto2.- The
, 3part 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:
--iandj++. - The
&&(logical AND) operator means the loop will continue as long as both--iandj++evaluate to non-zero (true).
Let's go through each iteration of the loop.
Step-by-Step Execution
First Iteration:
--i:iis decremented from2to1.j++:jis currently2, but afterj++, it increments to3.- Since
--iis1(true) andj++was2(also true), the loop condition is satisfied, and the loop body executes. printf("%d - %d", i, j);prints the current values ofiandj, which are1and3, 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






