strcpy() — Copy Strings

Format

#include <string.h>
char *strcpy(char *string1, const char *string2);

Language Level: ANSI

Threadsafe: Yes.

Description

The strcpy() function copies string2, including the ending null character, to the location that is specified by string1.

The strcpy() function operates on null-ended strings. The string arguments to the function should contain a null character (\0) that marks the end of the string. No length checking is performed. You should not use a literal string for a string1 value, although string2 may be a literal string.

Return Value

The strcpy() function returns a pointer to the copied string (string1).

Example that uses strcpy()

This example copies the contents of source to destination.

#include <stdio.h>
#include <string.h>
 
#define SIZE 40
 
int main(void)
{
  char source[SIZE] = "This is the source string";
  char destination[SIZE] = "And this is the destination string";
  char * return_string;
 
  printf( "destination is originally = \"%s\"\n", destination );
  return_string = strcpy( destination, source );
  printf( "After strcpy, destination becomes \"%s\"\n", destination );
}
 
/*****************  Output should be similar to:  *****************
 
destination is originally = "And this is the destination string"
After strcpy, destination becomes "This is the source string"
*/

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]