ProgrammingVery hard
Which condition makes a loop with an int index i and a size_t bound n safe from an infinite loop when n is 0?
- A. The loop wraps i to SIZE_MAX, so it runs forever
- B. The loop is undefined because size_t is unsigned
- C. The loop condition i < n is false from the start when n is 0, so the body never runs
- D. The compiler removes the loop, which makes the output undefined
Correct answer: C. The loop condition i < n is false from the start when n is 0, so the body never runs
A comparison with an unsigned zero bound fails at once. The mixed signed and unsigned comparison is worth checking, but the result here is well defined.
More C Programming questions
- A loop does for (int i = 1; i <= n; i++) { x = i / 2; } with n = 10^9. What is the main cost concern?
- Which tool gives the fastest I/O in C for up to 10^6 integers in a contest setting?
- Each of 10^5 insertions into a dynamic array takes an O(n) memmove to keep the array sorted. Which total cost is right?
- A program uses a 2D table of size 1000 x 1000 with an O(1) update per cell and 1000 passes. How many cell updates are done?
- The number of comparisons a merge of two sorted arrays of sizes 4 and 6 makes in the worst case is at most?
- struct P { int a[3]; }; struct P p = {{1, 2, 3}}; struct P q = &p; printf("%d", (q->a + 2)); What is printed?