Think of the difference between the source and the destination array, something is missing in the destination. The null-terminator.
Note: Both the arrays are local variable with automatic storage and unless initialized explicitly, their content is indeterminate.
Without a null-terminator in place, printf()
will go out of bound for the supplied array while printing with %s
which invokes undefined behavior.
The easiest way to handle this is zero-initilize the arrays, like
char x[25] = {0} ,y[25] = {0};
which makes all the elements of the arrays set to 0, and the same value being used as null-terminator, you are not required to add one manually to the destination array.
Also, FWIW,
You should length-limit the input to prevent buffer overflow from longer than expected input, using something along the line scanf("%24s",x);
better to use fgets()
to take the user input. If, iff, you have to use scanf()
,you can use it but please use proper error checking.