strcpy() — Copy Strings
Format
#include <string.h>
char *strcpy(char *string1, const char *string2);
Language Level
ANSI
Threadsafe
Yes
Description
The strcpy()
function
copies string2, including the ending null
character, to the location that is specified by string1.
The strcpy()
function
operates on null-ended strings. The string arguments to the function
should contain a null character (\0) that marks the end of
the string. No length checking is performed. You should not use a
literal string for a string1 value, although string2 may
be a literal string.
Return Value
The strcpy()
function
returns a pointer to the copied string (string1).
Example
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 should be similar to: *****************
destination is originally = "And this is the destination string"
After strcpy, destination becomes "This is the source string"
*/
Related Information
- strcat() — Concatenate Strings
- strchr() — Search for Character
- strcmp() — Compare Strings
- strcspn() — Find Offset of First Character Match
- strncpy() — Copy Strings
- strpbrk() — Find Characters in String
- strrchr() — Locate Last Occurrence of Character in String
- strspn() — Find Offset of First Non-matching Character
- wcscpy() — Copy Wide-Character Strings
- wcsncpy() — Copy Wide-Character Strings
- <string.h>