
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - strcpy() function
The C library strcpy() function accepts two parameter which copies the string pointed to, by src to dest. This function is essential for maintaining and upgrading of older system.
Syntax
Following is the syntax of C library strcpy() function −
char *strcpy(char *dest, const char *src)
Parameters
This function accepts the following parameters −
dest − This is the pointer to the destination array where the content is to be copied.
src − This is the string to be copied.
Ensure that the destination array has enough space to hold the source string, containing the null terminator.
Return Value
This returns a pointer to the destination string dest.
Example 1
Following is the basic C program that shows the usage of strcpy() function.
#include <stdio.h> #include <string.h> int main () { char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is tutorialspoint.com"); strcpy(dest, src); printf("Final copied string : %s\n", dest); return(0); }
Output
On execution of above code, we get the following output −
Final copied string : This is tutorialspoint.com
Example 2
Below the example shows the usage of custom string copy without the use of strcpy().
#include <stdio.h> void customStrcpy(char* dest, const char* src) { while (*src) { *dest = *src; dest++; src++; } *dest = '\0'; } int main() { char source[] = "string Copy!"; char destination[20]; customStrcpy(destination, source); printf("The custom copied string: %s\n", destination); return 0; }
Output
After executing the above code, we get the following output −
The custom copied string: string Copy!
Example 3
Here, we define the two strings variable − source and destination. Then apply strcpy() which accepts both these variable to determine the result of copy string.
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello, World!"; char destination[20]; strcpy(destination, source); printf("The result of copied string: %s\n", destination); return 0; }
Output
The above code produces the following output −
The result of copied string: Hello, World!