strcpy() in C/C++


The function strcpy() is a standard library function. It is used to copy one string to another. In C language,it is declared in “string.h” header file while in C++ language, it is declared in cstring header file. It returns the pointer to the destination.

Here is the syntax of strcpy() in C language,

char* strcpy(char* dest, const char* src);

Some key points of strcpy().

  • It copies the whole string to the destination string. It replaces the whole string instead of appending it.

  • It won’t change the source string.

Here is an example of strcpy() in C language,


 Live Demo

#include <stdio.h>
int main() {
   char s1[] = "Hello world!";
   char s2[] = "Welcome";
   printf("String s1 before: %s
", s1);    strcpy(s1, s2);    printf("String s1 after: %s
", s1);    printf("String s2 : %s", s2);    return 0; }


String s1 before: Hello world!
String s1 after: Welcome
String s2 : Welcome
Updated on 24-Jun-2020 11:06:39