ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
/* 16-bit unsigned short, 32-bit int and unsigned */
unsigned short a = 3, b = 5;
printf("%d %u %d", a - b, 3u - 5u, (unsigned short)(a - b));- A. 65534 4294967294 65534
- B. -2 -2 -2
- C. -2 4294967294 -2
- D. -2 4294967294 65534
Correct answer: D. -2 4294967294 65534
unsigned short is narrower than int, so both operands are promoted to (signed) int and a − b is simply −2. With genuine unsigned ints, 3u − 5u wraps modulo 2^32 to 4294967294. Casting −2 to unsigned short wraps modulo 2^16 to 65534, which is then promoted back to int for printf.
More C Programming questions
- What is printed? c #define ABS(x) x < 0 ? -x : x printf("%d %d", ABS(2 - 5), ABS(-4) 2);
- What is printed? c const char s = "mississippi"; printf("%td %td %td", strchr(s, 's') - s, strrchr(s, 's') - s, strstr(s, "ssip") - s);
- What is printed? c / 4-byte int / struct V { int a[3]; }; void f(struct V v) { v.a[0] = 9; } void g(int a[3]) { a[0] = 9; } int main(void)…
- What is printed? c int n = 10, c = 0; int k = (n + 3) / 4; switch (n % 4) { case 0: do { c++; case 3: c++; case 2: c++; case 1: c++; }…
- What is printed? c int calls = 0; int ways(int n) { calls++; if (n < 0) return 0; if (n == 0) return 1; return ways(n - 1) + ways(n - 3) +…
- What is printed? c int moves = 0; void h(int n, char from, char via, char to) { if (n == 0) return; h(n - 1, from, to, via); if (++moves…