strcpy Function

Copy the source string into the destination string.

Include

<string.h>

Prototype

char *strcpy(char *s1, const char *s2);

Arguments

s1
destination string to copy to
s2
source string to copy from

Return Value

Returns a pointer to the destination string.

Remarks

All characters of s2 are copied, including the null terminating character. If the strings overlap, the behavior is undefined.

For MPLAB XC16 functions that can copy to/from specialized memory areas, such as strcpy_eds or strcpy_packed, see “Functions for Specialized Copying and Initialization” in the MPLAB XC16 Libraries Reference Guide.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

#include <string.h>
#include <stdio.h>

int main(void)
{
  char buf1[50] = "We're here";
  char buf2[50] = "Where is the time?";
  char buf3[50] = "Why?";

  printf("buf1 : %s\n", buf1);
  printf("buf2 : %s\n", buf2);
  printf("buf3 : %s\n\n", buf3);

  strcpy(buf1, buf2);
  printf("buf1 after strcpy of buf2: \n\t%s\n\n",
         buf1);

  strcpy(buf1, buf3);
  printf("buf1 after strcpy of buf3: \n\t%s\n",
          buf1);
}

Example Output

buf1 : We're here
buf2 : Where is the time?
buf3 : Why?

buf1 after strcpy of buf2:
        Where is the time?

buf1 after strcpy of buf3:
        Why?