strcpy() — Copy string
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
Format
#include <string.h>
char *strcpy(char * __restrict__string1, const char * __restrict__string2);
General description
The strcpy() built-in function copies string2, including the ending NULL character, to the location specified by string1. The string2 argument to strcpy() must contain a NULL character (\0) marking the end of the string. You cannot use a literal string for a string1 value, although string2 may be a literal string. If the two objects overlap, the behavior is undefined.
Returned value
strcpy() returns the value of string1.
Example
CELEBS38
/* CELEBS38
This example copies the contents of source to destination.
*/
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char source[ SIZE ] = "This is the source string";
char destination[ SIZE ] = "And this is the destination string";
char * return_string;
printf( "destination is originally = \"%s\"\n", destination );
return_string = strcpy( destination, source );
printf( "After strcpy, destination becomes \"%s\"\n", destination );
}
Output
destination is originally = "And this is the destination string"
After strcpy, destination becomes "This is the source string"