#include <stdio.h>
#include <string.h>
int main()
{
char s1[] = "harry";
char s2[] = "ravi";
char s3[54];
puts(strcat(s1, s2));
strcpy(s3 ,strcat(s1, s2));
puts(s3);
return 0;
}
this is the error that I will get
cd "/Users/deepkar/Desktop/C_COURSE/" && gcc tut27.c -o tut27 && "/Users/deepkar/Desktop/C_COURSE/"tut27
deepkar@Deeps-Air C_COURSE % cd "/Users/deepkar/Desktop/C_COURSE/" && gcc tut27.c -o tut27 && "/Users/deepkar/Desktop/C_COURSE/"tut27
zsh: illegal hardware instruction "/Users/deepkar/Desktop/C_COURSE/"tut27
deepkar@Deeps-Air C_COURSE %
The problem come from your code having undefined behavior:
You call
strcat(s1, s2);twice, attempting to copy the characters froms2at the end of the array pointed to bys1, buts1already contains a string"harry"that along with its null terminator fills the array completely, the definitionchar s1[] = "harry";giving it a length of 6 bytes.strcatwrites beyond the end of the arrays1, overwriting important data, such as the return address for functionmain. The observed behavior is consistent with the CPU branching to an invalid address.You can copy
s1tos3and catenates2to that instead.