ProgrammingHardTCS NQT · Accenture

What is printed? The hexadecimal literal is 0x1F and the comparison is with 31.

What is printed? The hexadecimal literal is 0x1F and the comparison is with 31.
#include <stdio.h>
int main(void) {
    printf("%d", 0x1F == 31);
}
  1. A. 0
  2. B. 2
  3. C. 3
  4. D. 1

Correct answer: D. 1

0x1F = 1 x 16 + 15 = 31, so the comparison is true, and == always yields the int 1 (or 0). Answers like 2 or 3 confuse the comparison result with some other value.

More C Programming questions

Exams that ask this topic