ProgrammingVery hardProduct company OA

Assume 8-bit char, 16-bit unsigned short and 32-bit int. What is printed?

Assume 8-bit char, 16-bit unsigned short and 32-bit int. What is printed?
int big = 300;
unsigned char uc = big;
unsigned short us = -1;
unsigned int u = us + 1;
unsigned short t = us + 1;
printf("%d %u %u %d", uc, u, t, (unsigned char)(uc * 6));
  1. A. 44 65536 0 8
  2. B. 44 0 0 8
  3. C. 300 65536 0 264
  4. D. 44 65536 65536 8

Correct answer: A. 44 65536 0 8

Conversion to an unsigned type is reduction modulo 2ⁿ: 300 → 44, −1 → 65535. In us + 1 the unsigned short is promoted to int (int can hold all its values), so the sum is the int 65536 – no wrap – and u keeps 65536; only storing back into the 16-bit t reduces it to 0. uc * 6 = 264 as an int, truncated to 8. The option with u = 0 assumes arithmetic is done in the narrow type.

More C Programming questions

Exams that ask this topic