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[] = "kernel"; s[1] = '\0'; printf("%zu %zu %s", strlen(s), sizeof(s), s + 0); return 0; }

  1. A. 6 7 k
  2. B. 1 7 k
  3. C. 2 7 ke
  4. D. 1 7 kernel

Correct answer: B. 1 7 k

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 "k".

More C Programming questions

Exams that ask this topic