This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.

Released under the GNU General Public License.

Return to index Return to header Previous symbol Next symbol

strcpy

Prototype

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

Description

Copies the specified string src including the terminating null character in the buffer dest overwriting the previous existing data. The dest buffer should be large enough to contain the new string. The two strings should not overlap. Returns the dest parameter.
For example, in the following code we copy a string into an array:
char buf[12];
strcpy(buf, "hello world");
this is the state of the buf array before and after the call of strcpy (? means any character):

Implementation

View source
#include <string.h>

char *strcpy(char *dest, const char *src)
{
    char *dp = dest;

    while ((*dp++ = *src++) != '\0')
	;

    return dest;
}

Example

View source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char buf[12];

    strcpy(buf, "Hello world");
    printf("%s\n", buf);	/* Prints "Hello world". */

    return EXIT_SUCCESS;
}

References

ISO C 9899:1990 7.11.2.3