ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
enum Color { RED, GREEN, BLUE };
enum Color c = GREEN;
printf("%d", c);
enum Status { OK = 1, FAIL = 5, PENDING };
printf(" %d", PENDING);- A. 1 6
- B. 0 6
- C. 1 5
- D. 2 6
Correct answer: A. 1 6
Unscoped enum constants default to 0,1,2,... unless given explicit values: RED=0, GREEN=1, BLUE=2, so c (GREEN) prints 1. In the second enum, OK=1 (explicit), FAIL=5 (explicit), and PENDING (no explicit value) continues from the PREVIOUS constant + 1: FAIL+1 = 6.
More C Programming questions
- What is printed? c int main(void) { int x = 1; int p = &x; p += 1; (p)++; ++p; printf("%d", x); }
- What is printed? c int main(void) { int x = 0; int y = (x = 5, x + 1); printf("%d %d", x, y); }
- What is printed? c int main(void) { int arr[3] = {1, 2, 3}; int p = arr; for (int i = 0; i < 3; i++, p++) { printf("%d ", p); } }
- Assume 32-bit int. What is printed? c int x = 7; int y = x > 5 && x < 10 ? x 2 : x + 1; printf("%d", y);
- What happens when this runs? c int p = malloc(10 sizeof(int)); for (int i = 0; i <= 10; i++) { p[i] = i; } free(p);
- Assume 32-bit int. What is printed? c int a = 6; int b = a++ 2; printf("%d %d", a, b);