ProgrammingVery hardTCS NQT · Infosys
For unsigned int i = 3; i >= 0; i--, what happens to the loop body?
- A. It runs 4 times
- B. It loops forever
- C. It is a compile error
- D. It is undefined behaviour
Correct answer: B. It loops forever
An unsigned value is never negative, so i >= 0 is always true. When i wraps from 0 to UINT_MAX the loop goes on, so it never ends. The wrap itself is well defined.
More C Programming questions
- char a[] = "hi"; char b[] = "hi"; What does a == b evaluate to?
- What does printf("%d", '0' < '9') print?
- What does printf("%d", 6 % 4 2) print?
- Which expression computes ceil(a / b) for positive int a = 7 and b = 3 using integer arithmetic, written as ((a + b - 1) / b)?
- int a[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) a[i][j] = i 4 + j; printf("%d", (&a[0][0] + 7)); What is printed?
- With int a[3][4] (4-byte int, row-major), what is the value of (int )&a[1][0] - (int )&a[0][0]?