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));- A. 44 65536 0 8
- B. 44 0 0 8
- C. 300 65536 0 264
- 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
- What is printed? c int slot(int i) { static int box[3]; return &box[i % 3]; } int main(void) { slot(0) = 5; slot(4) = 7; int p = slot(3)…
- What is printed? c #define A 1 #define B #if defined(B) && A + C #define X 10 #else #define X 20 #endif #undef A #ifdef A #define Y 1…
- An array of int that may contain any values from INTMIN to INTMAX is sorted with qsort. With int x = (const int )a, y = (const int )b…
- What is printed? c int a[] = {4, 8, 15, 16, 23}; printf("%zu %d", sizeof(a) / sizeof(a[0]), a[sizeof(a) / sizeof(a[0]) - 1]);
- What is printed? c printf("%d %d %d", 5 > 3 && 2 > 4, !0, !!7);
- What is printed? c int a = 5, b = 3; a = a ^ b; b = a ^ b; a = a ^ b; printf("%d %d", a, b);