C strcpy()
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 pokemon1strcpy(pokemon1, pokemon2);printf("pokemon1 is now: %s", pokemon1);return 0;}
The output would be:
Pikachu
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours