char * a = (char *) malloc(10); strcpy(a,"string1"); char * x = "string2"; strcat(a,x); printf("\n%s",a); Here, I allocated only 10B to a, but still after concatenating a and x (combined size is 16B), C prints the answer without any problem.
But if I do this:
char * a = "string1"; char * x = "string2"; strcat(a,x); printf("\n%s",a); Then I get a segfault. Why is this? Why does the first one work despite lower memory allocation? Does strcat reallocate memory for me? If yes, why does the second one not work? Is it because a & x declared that way are unmodifiable string literals?