ProgrammingVery hardProduct company OA
Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed?
Assume plain char is signed 8-bit, two's complement, and EOF is −1. What is printed?
char c = '\xff';
printf("%d %d %d %d", c == 0xFF, (unsigned char)c == 0xFF, c == EOF, c + 1);- A. 1 1 0 256
- B. 0 1 0 0
- C. 1 1 1 0
- D. 0 1 1 0
Correct answer: D. 0 1 1 0
With a signed char, the byte 0xFF holds −1. In every comparison c is promoted to int with sign extension, so c == 0xFF compares −1 with 255 → 0, while c == EOF is −1 == −1 → 1. Converting to unsigned char first gives 255 → 1. c + 1 = 0. This is why getchar() must be stored in an int: a char cannot distinguish the valid byte 0xFF from EOF.
More C Programming questions
- Read the declaration: c char ((x)[4])(int); What is x?
- What is printed? c char a[8] = "abcdefg"; memmove(a + 2, a, 4); char b[6] = "XXXXX"; strncpy(b, "hi", 4); char c[4]; strncpy(c, "wxyz"…
- What is printed? c struct N { int v; struct N next; }; struct N n[5] = {{1, n + 1}, {2, n + 2}, {3, n + 3}, {4, n + 4}, {5, 0}}; struct N…
- What is printed? c int lb(const int a, int n, int x) { int lo = 0, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (a[mid] < x)…
- What is printed? c int a = 7, b = 2; double x = a / b; double y = (double)a / b; double z = (double)(a / b); int w = (int)(-3.7) +…
- What is printed? c enum { RED = 2, GREEN, BLUE = GREEN + 4, N }; int a[] = { [RED] = 5, [BLUE] = 1, 9, [GREEN] = 6, 4 }; printf("%zu %d %d…