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 = "hello, world";
        char str1[15] = "hello wo 9";
        strcpy(str, str1);
        printf("%s", str1);
    }

 

  • A.Compilation error
  • B.Segmentation Fault
  • C.hello, world
  • D.hello, wo 9

Answer: B

Correct answer: Segmentation Fault

str points to a string literal, which almost every compiler places in read-only memory. strcpy then tries to write over it, and the program is killed by the memory protection. Declaring char str[20] = "hello, world"; would give a writable copy.

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

Post your comment

Please log in or register to post a comment.