ProgrammingHardGATE CS · Campus placements

int a = -1; unsigned int b = 1u; What does if (a < b) evaluate to?

  1. A. true, because -1 is less than 1
  2. B. false, because a is converted to unsigned int and becomes UINT_MAX
  3. C. a compile-time error
  4. D. undefined behaviour

Correct answer: B. false, because a is converted to unsigned int and becomes UINT_MAX

In a relational comparison the usual arithmetic conversions turn -1 into the unsigned value UINT_MAX (for a 32-bit unsigned int, 4294967295). That is larger than 1, so the test is false. The conversion is well defined.

More C Programming questions

Exams that ask this topic