why does this code show the following output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why does this code show the following output?

code: #include <stdio.h> #include <string.h> int main() { char s1[ ] = "The grey fox"; char s2[ ] = " jumped."; strcat(s1, s2); printf("%s\n", s1); printf("Length of s1 is %d\n", strlen(s1)); strcpy(s1, s2); printf("s1 is now %s \n", s1); return 0; } output: The grey fox jumped. Length of s1 is 20 s1 is now jumped. *** stack smashing detected ***: <unknown> terminated Aborted (core dumped)

28th Apr 2020, 7:25 PM
Neha Ann Jacob
2 Answers
+ 4
char s1[ ] = "The grey fox"; s1 is char array of 13 bytes when you call strcat(s1, s2); you do buffer overflow because your need 21 byte to store concatenated string but s1 can contain only 13. declare s1 as char s1[50] = "The grey fox"; to reserve space for the enlarged string.
28th Apr 2020, 7:40 PM
andriy kan
andriy kan - avatar
+ 2
test
28th Apr 2020, 9:26 PM
HonFu
HonFu - avatar