ProgrammingVery hardGATE CS
What is printed?
What is printed?
#include <stdio.h>
int f(int x) {
int r = 0;
switch (x) {
case 1: r += 1;
default: r += 10;
case 2: r += 2; break;
case 3: r += 3;
}
return r;
}
int main(void) {
printf("%d %d %d %d", f(1), f(2), f(3), f(4));
return 0;
}- A. 11 2 3 10
- B. 1 2 3 12
- C. 13 2 6 12
- D. 13 2 3 12
Correct answer: D. 13 2 3 12
Without break, execution falls through: f(1) runs case 1, default and case 2 (1 + 10 + 2 = 13); f(2) adds 2 and breaks; f(3) adds 3 and leaves the switch; f(4) jumps to default, then falls into case 2: 10 + 2 = 12.
More C Programming questions
- What is the exact output? c #include <stdio.h> void p(int n) { if (n <= 0) return; printf("%d", n); p(n - 1); printf("%d", n); p(n - 2); }…
- What is printed? Assume int is 4 bytes and the array is a 2-D array object of automatic storage. c #include <stdio.h> int main(void) { int…
- What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation. c #include <stdio.h> int…
- What is printed? c int a[5] = {1, 2}; printf("%d %d %zu", a[1], a[2], sizeof a / sizeof a[0]);
- What is printed? c printf("%.1f %d", 7 / 2 2.0, (int)(7 / 2.0 2));
- What is printed? c printf("%d %d", 6 & 3 << 1, ~5 + 1);