strcpy, wcscpy, _mbscpy
Copies a string. More secure versions of these functions are available; see strcpy_s, wcscpy_s, _mbscpy_s.
Important
|
|---|
|
_mbscpy cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW. |
char *strcpy( char *strDestination, const char *strSource ); wchar_t *wcscpy( wchar_t *strDestination, const wchar_t *strSource ); unsigned char *_mbscpy( unsigned char *strDestination, const unsigned char *strSource ); template <size_t size> char *strcpy( char (&strDestination)[size], const char *strSource ); // C++ only template <size_t size> wchar_t *wcscpy( wchar_t (&strDestination)[size], const wchar_t *strSource ); // C++ only template <size_t size> unsigned char *_mbscpy( unsigned char (&strDestination)[size], const unsigned char *strSource ); // C++ only
The strcpy function copies strSource, including the terminating null character, to the location that's specified by strDestination. The behavior of strcpy is undefined if the source and destination strings overlap.
Security Note
|
|---|
|
Because strcpy does not check for sufficient space in strDestination before it copies strSource, it is a potential cause of buffer overruns. Therefore, we recommend that you use strcpy_s instead. |
wcscpy and _mbscpy are, respectively, wide-character and multibyte-character versions of strcpy. The arguments and return value of wcscpy are wide-character strings; those of _mbscpy are multibyte-character strings. These three functions behave identically otherwise.
In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. For more information, see Secure Template Overloads.
|
TCHAR.H routine |
_UNICODE & _MBCS not defined |
_MBCS defined |
_UNICODE defined |
|---|---|---|---|
|
_tcscpy |
strcpy |
_mbscpy |
wcscpy |
|
Routine |
Required header |
|---|---|
|
strcpy |
<string.h> |
|
wcscpy |
<string.h> or <wchar.h> |
|
_mbscpy |
<mbstring.h> |
For additional compatibility information, see Compatibility.
// crt_strcpy.c
// compile with: /W3
// This program uses strcpy
// and strcat to build a phrase.
#include <string.h>
#include <stdio.h>
int main( void )
{
char string[80];
// If you change the previous line to
// char string[20];
// strcpy and strcat will happily overrun the string
// buffer. See the examples for strncpy and strncat
// for safer string handling.
strcpy( string, "Hello world from " ); // C4996
// Note: strcpy is deprecated; use strcpy_s instead
strcat( string, "strcpy " ); // C4996
// Note: strcat is deprecated; use strcat_s instead
strcat( string, "and " ); // C4996
strcat( string, "strcat!" ); // C4996
printf( "String = %s\n", string );
}
String = Hello world from strcpy and strcat!
Important