ProgrammingVery hard
unsigned int i; for (i = 10; i >= 0; i--) { } What is the behaviour of this loop?
- A. It runs 11 times and stops
- B. It is undefined behaviour because i wraps
- C. It never terminates, because an unsigned value is never negative, so i >= 0 is always true
- D. It runs 10 times and stops
Correct answer: C. It never terminates, because an unsigned value is never negative, so i >= 0 is always true
i >= 0 is always true for an unsigned type, and i wraps from 0 to UINT_MAX, so the loop never ends. The standard defines the wrap, so the loop is an infinite loop, not undefined.
More C Programming questions
- Which C code correctly counts the digits of n = 10^9 in a fast enough way for many queries?
- The sum of the digits of n = 10^9 - 1 is computed with a loop that divides by 10 each time. How many loop iterations run?
- To list all primes up to 10^7 in C with memory and time that fit, which is the best choice?
- For each of Q = 10^5 queries, a program sums an array prefix of length up to N = 10^5 by a loop. What is the total cost?
- A recurrence T(n) = T(n - 1) + n is solved by a program with a loop over the recursion. What is the total cost in terms of n?
- What is the total number of elements in a triangle of rows 1 to 10, where row k has k elements?