ProgrammingVery hardProduct company OA
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
char s[] = "a\0bc";
char *p = s;
char m[3][5];
printf("%zu %zu %zu %zu %zu %zu", sizeof s, strlen(s), sizeof p, sizeof *m, sizeof m[0][0] + sizeof(m) / sizeof(m[0]), sizeof("ab" "cd"));- A. 5 1 8 5 4 4
- B. 4 4 8 15 4 5
- C. 2 1 5 5 4 5
- D. 5 1 8 5 4 5
Correct answer: D. 5 1 8 5 4 5
The array s gets every character of the literal plus the implicit terminator: 'a', '\0', 'b', 'c', '\0' → 5 bytes, while strlen stops at the first '\0' → 1. p is just a pointer (8). *m is the first row, a char[5] → 5. 1 + 15/5 = 4. Adjacent literals are concatenated at translation time, so sizeof("abcd") = 5 including its '\0'. Options with 4 for the last value forget the terminator; '4 4' forgets that the embedded '\0' still occupies space but ends strlen.
More C Programming questions
- What is printed? c int inc(int x) { return x + 1; } int dbl(int x) { return x 2; } int sqr(int x) { return x x; } int apply(int…
- What is printed? c int s = 0; for (int i = 0; i < 6; i++) { switch (i % 4) { case 0: s += 1; case 1: s += 2; break; case 2: continue…
- What is printed? c int calls; int f(int n) { calls++; return n < 3 ? n : f(n - 1) + f(n - 3); } int main(void) { int r = f(8); printf("%d…
- What is printed? c char s[] = "aaabccdddd"; char r = s, w = s; while (r) { char c = r; int n = 0; while (r == c) { r++; n++; } w++ = c; if…
- Assume 32-bit unsigned int. What is printed? c unsigned int x = 0x5Cu; / 0101 1100 / unsigned int low = x & -x; unsigned int y = x + low…
- Assume 4-byte int with 4-byte alignment. What is printed? c struct P { char tag; int v; }; struct P a[4] = {{'a', 1}, {'b', 2}, {'c', 3}…