strcpy, strcpy_s

Header: <string.h>

  1. Copies the null-terminated byte string pointed to by src, including the null terminator, to the character array whose first element is pointed to by dest.

# Declarations

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

(until C99)

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

(since C99)

errno_t strcpy_s( char *restrict dest, rsize_t destsz, const char *restrict src );

(since C11)

# Parameters

# Notes

strcpy_s is allowed to clobber the destination array from the last character written up to destsz in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.

The function strcpy_s is similar to the BSD function strlcpy, except that

Although strcpy_s prohibits truncation due to potential security risks, it’s possible to truncate a string using bounds-checked strncpy_s instead.

# Example

#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    const char *src = "Take the test.";
//  src[0] = 'M' ; // this would be undefined behavior
    char dst[strlen(src) + 1]; // +1 to accommodate for the null terminator
    strcpy(dst, src);
    dst[0] = 'M'; // OK
    printf("src = %s\ndst = %s\n", src, dst);
 
#ifdef __STDC_LIB_EXT1__
    set_constraint_handler_s(ignore_handler_s);
    int r = strcpy_s(dst, sizeof dst, src);
    printf("dst = \"%s\", r = %d\n", dst, r);
    r = strcpy_s(dst, sizeof dst, "Take even more tests.");
    printf("dst = \"%s\", r = %d\n", dst, r);
#endif
}

# See also