The strcpy() function is used in C language to copy one string to another string. Its usage is as follows: declare two string variables, one to store the target string and the other to store the source string. Initialize source string. Call the strcpy() function to copy the source string to the destination string.

Usage of strcpy function in C language
The strcpy() function is used in C language to convert a Copy a string into another string. The syntax is as follows:
char *strcpy(char *dest, const char *src);
Copy after login
Among them:
- dest: target string, used to store the copied string.
- src: Source string, containing the string to be copied.
How to use strcpy() function?
- Declare two string variables: One is used to store the target string and the other is used to store the source string.
- Initialize the source string: Use a string constant or other string assignment to initialize the source string.
- Call the strcpy() function: Use the strcpy() function to copy the source string to the target string, and store the return value of the function in a pointer to the target string .
Sample code:
#include <stdio.h>
#include <string.h>
int main() {
char dest[20];
char src[] = "Hello World!";
strcpy(dest, src);
printf("%s\n", dest);
return 0;
}
Copy after login
Key points:
- strcpy() function will overwrite the target The existing content in the string.
- The source string must end with a null character ('\0').
- The strcpy() function returns the address of the target string so that it can be processed further.
- Do not point the source string and target string to the same memory location, otherwise undefined behavior will occur.
The above is the detailed content of How to use strcpy in c language. For more information, please follow other related articles on the PHP Chinese website!