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"));
  1. A. 5 1 8 5 4 4
  2. B. 4 4 8 15 4 5
  3. C. 2 1 5 5 4 5
  4. 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

Exams that ask this topic