ProgrammingHardTCS NQT · Infosys
unsigned int n = 0u; int i = 0; if (i < n - 1) printf("loop"); What is printed?
- A. nothing
- B. loop
- C. -1
- D. undefined
Correct answer: B. loop
n - 1 wraps to UINT_MAX, and i is converted to unsigned for the comparison, so 0 < UINT_MAX is true. Unsigned counters like this are a classic bug in loops over sizes.
More C Programming questions
- Is the C standard guaranteed to remove stack frames of tail-recursive calls, so that deep tail recursion cannot overflow the stack?
- int a[6] = {1, 2, 3, 4, 5, 6}; int p = a, q = a + 5; while (p < q) { int t = p; p++ = q; q-- = t; } What is a[1] afterwards?
- uint8t x = 0xB4; What is the lowest set bit of x, computed as x & (uint8t)-x?
- uint8t rev(uint8t x) { uint8t r = 0; for (int i = 0; i < 8; i++) { r = (uint8t)((r << 1) | (x & 1)); x >>= 1; } return r; } What is…
- int a[] = {4, 9, 16}; int p = a; What is (p + 2)[-1]?
- To compute a b mod m with 0 <= a, b < m <= 1000000007 in a competitive program, which expression is correct?