Your question is not clear, since the code makes little or no sense. Your input cannot be longer than 20 characters since the receiving array is only 20 characters. If the user inputs more, your program will produce undefined behavior. So, the main problem here is not limiting the copy, but rather limiting the input.
However, your question seems to be about limited-length string copying. If that's what you need, then unfortunately there no dedicated function in standard library for that purpose. Many implementation provide the non-standard strlcpy
function that does exactly that. So, either check if your implementation provides strlcpy
or implement your own strlcpy
yourself.
In many cases you might see advices to use strncpy
in such cases. While it is possible to beat strncpy
into working for this purpose, in reality strncpy
is not intended to be used that way. Using strncpy
as a limited-length string copying function is always an error. Avoid it.