I am new to C and am taking the C course by the C++ Institute. It seems I am not as smart as I thought.
This program returns 4, but I cannot understand why:
Code:
#include <stdio.h>
int main(void) {
int i = 3, j = i - 2;
switch(i - 2) {
case 1: j++;
case 2: j++;
case 0: j++; break;
default:j = 0;
}
printf("%d",j);
return 0;
}
It seems that j increments at each case statement. But why is it so?
I would expect the i - 2 condition to evaluate to 1, the program to execute case 1: j++, then printf("%d",j); to be called, with j = 2.