ProgrammingVery hardGATE CS

What is printed? Assume pointers are 8 bytes and the string literal's characters are those between the quotes.

What is printed? Assume pointers are 8 bytes and the string literal's characters are those between the quotes.
#include <stdio.h>
#include <string.h>
int main(void) {
    char s[] = "ab\0cd";
    char *p = "ab\0cd";
    printf("%zu %zu %zu %zu", sizeof(s), strlen(s), sizeof(p), strlen(p + 3));
    return 0;
}
  1. A. 5 2 8 2
  2. B. 6 2 5 2
  3. C. 6 5 8 2
  4. D. 6 2 8 2

Correct answer: D. 6 2 8 2

The array s holds a, b, \0, c, d and the final terminator: sizeof(s) = 6, while strlen stops at the first \0 and gives 2. sizeof(p) is the pointer size (8), and p + 3 points to "cd", so strlen is 2.

More C Programming questions

Exams that ask this topic