strcpy()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 25, 2021Updated Aug 13, 2022
Contribute to Docs

The strcpy() function copies one string into another string, and returns the newly copied string.

Syntax

#include <string.h>

strcpy(string1, string2);

At the top of the file, the <string.h> header file needs to be included. The strcpy() copies string2 parameter into string1, including the terminating null character ('\0').

Example

Suppose we have two strings, pokemon1 and pokemon2:

#include <stdio.h>
#include <string.h>
int main() {
char pokemon1[30] = "Ditto";
char pokemon2[30] = "Pikachu";
// This function has copied pokemon2 into pokemon1
strcpy(pokemon1, pokemon2);
printf("pokemon1 is now: %s", pokemon1);
return 0;
}

The output would be:

Pikachu

All contributors

Contribute to Docs

Learn C on Codecademy