ProgrammingHardTCS NQT · Infosys · Product Company SDE

What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits)? #include <stdio.h> #include <string.h> int main(void) { char s[] = "laptop"; s[1] = '\0'; printf("%zu %zu %s", strlen(s), sizeof(s), s + 0); return 0; }

  1. A. 1 7 laptop
  2. B. 1 7 l
  3. C. 2 7 la
  4. D. 1 6 l

Correct answer: B. 1 7 l

s[1] = '\0' ends the string after 1 characters, so strlen = 1; sizeof still reports the array size 7 (including the original terminator); printf("%s") prints "l".

More C Programming questions

Exams that ask this topic