ProgrammingHardGATE CS

What is printed? Assume int and unsigned int are 32 bits.

What is printed? Assume int and unsigned int are 32 bits.
#include <stdio.h>
int main(void) {
    int a = -1;
    unsigned int b = 1;
    printf("%d %d %d", a < b, a < (int)b, (long long)a < (long long)b);
    return 0;
}
  1. A. 0 1 1
  2. B. 1 0 0
  3. C. 0 0 1
  4. D. 1 1 1

Correct answer: A. 0 1 1

In a < b the int is converted to unsigned, becoming 4294967295, which is not less than 1, so the first result is 0. With an explicit int conversion, or comparing as long long, the comparison is -1 < 1, which is true.

More C Programming questions

Exams that ask this topic