ProgrammingVery hardProduct company OA

Assume 32-bit int and 64-bit long long. What is printed?

Assume 32-bit int and 64-bit long long. What is printed?
int n = -1;
unsigned int len = 3;
long long big = -1;
printf("%d %d %d %d", n < len, n < (int)len, big < len, (n + len) == 2);
  1. A. 1 1 1 1
  2. B. 0 1 0 1
  3. C. 0 1 1 1
  4. D. 0 1 0 0

Correct answer: C. 0 1 1 1

In int vs unsigned int the int is converted to unsigned: −1 becomes 4294967295, so n < len is 0 (the classic loop-bound bug); with the cast it is 1. long long can represent every unsigned int value, so there len is converted to long long and −1 < 3 is 1 – the rule depends on rank, not just on 'unsigned wins'. n + len is computed modulo 2³² = 2. Output 0 1 1 1.

More C Programming questions

Exams that ask this topic