Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int main() {
int n = 2;
switch (n) {
case 1: std::cout << 'a'; break;
case 2: std::cout << 'b';
case 3: std::cout << 'c';
default: std::cout << 'd';
}
}bcd
- Expecting
b: acaselabel does not end the code above it, so withoutbreakexecution runs on intocase 3and further. - Expecting
bc: falling through does not stop atdefault, which is a label like anycase, sodis printed too.
- Does
[[fallthrough]]change what this program prints, and why would you add it? - Why may a variable declared right after a
caselabel, without braces, cause a compile error?