int main()
{
char s[5]={'s','a','\0','c','h'};
char p[5];
char t[5];
strcpy(p,s);
memcpy(t,s,5);
printf("sachin p is [%s], t is [%s]",p,t);
return 0;
}
The main difference is that memcpy() always copies the exact number of bytes you specify; strcpy(), on the other hand, will copy until it reads a NUL (aka 0) byte, and then stop after that.
Because of the null character in your s string, the printf won't show anything beyond that. The difference between p and t will be in characters 4 and 5. p won't have any (they'll be garbage) and t will have the 'c' and 'h'.
strcpy terminates when the source string's null terminator is found. memcpy requires a size parameter be passed. In the case you presented the printf statement is halting after the null terminator is found for both character arrays, however you will find t[3] and t[4] have copied data in them as well.
The problem with your test-program is, that the printf() stops inserting the argument into %s, when it encounters a null-termination \0.
So in your output you probably have not noticed, that memcpy() copied the characters c and h as well.
I have seen in GNU glibc-2.24, that (for x86) strcpy() just calls memcpy(dest, src, strlen(src) + 1).