ProgrammingModerateAccenture · Cognizant pattern
What happens?
What happens?
for (unsigned int i = 3; i >= 0; i--)
printf("%u ", i);- A. Prints 3 2 1 0
- B. Compilation error
- C. Prints 3 2 1
- D. Infinite loop: 3 2 1 0 4294967295 ... (32-bit unsigned)
Correct answer: D. Infinite loop: 3 2 1 0 4294967295 ... (32-bit unsigned)
An unsigned value is never negative, so i >= 0 is always true. After 0, i-- wraps around to UINT_MAX and the loop never ends. Use a signed counter or test i-- > 0.
More C Programming questions
- What is printed? c char s[] = "abc0def"; printf("%zu %zu", sizeof(s), strlen(s));
- What is printed? c printf("%zu", strlen("atbn"));
- What is printed? c int a = -1, b = 1; if (a > 0) if (b > 0) printf("X"); else printf("Y"); printf("Z");
- What is printed? c int x = 4; if (x & 1 == 0) printf("even"); else printf("odd");
- What is printed? c int f(int n) { static int s = 0; s += n; if (n > 0) f(n - 1); return s; } ... printf("%d ", f(3)); printf("%d", f(3));
- What is printed? c printf("%d", 2 + 3 << 1 & 7);