Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        char str[11] = "hello";
        char *str1 = "world";
        strcat(str, str1);
        printf("%s %d", str, str[10]);
    }

 

  • A.helloworld 0
  • B.helloworld anyvalue
  • C.worldhello 0
  • D.Segmentation fault/code crash

Answer: A

Correct answer: helloworld 0

strcat appends world to hello, giving ten characters, and writes the terminating null at index 10 - the last element of the eleven-byte array. So str[10] is '\0', and %d prints its value, 0. The array is exactly big enough; a char str[10] would have overflowed.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.