strcpy, wcscpy, _mbscpy
Copy a string. These functions are deprecated because more secure versions are available; see strcpy_s, wcscpy_s, _mbscpy_s.
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
Parameters
- strDestination
-
Destination string.
- strSource
-
Null-terminated source string.
The strcpy function copies strSource, including the terminating null character, to the location 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 copying strSource, it is a potential cause of buffer overruns. Consider using strcpy_s instead. |
wcscpy and _mbscpy are 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 | Compatibility |
|---|---|---|
| strcpy | <string.h> | ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
| wcscpy | <string.h> or <wchar.h> | ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
| _mbscpy | <mbstring.h> | Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 |
For additional compatibility information, see Compatibility in the Introduction.
// 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];
// Note that 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; consider using strcpy_s instead
strcat( string, "strcpy " ); // C4996
// Note: strcat is deprecated; consider using strcat_s instead
strcat( string, "and " ); // C4996
strcat( string, "strcat!" ); // C4996
printf( "String = %s\n", string );
}
Output
String = Hello world from strcpy and strcat!
Reference
String Manipulation (CRT)strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strncat, _strncat_l, wcsncat, wcsncat_l, _mbsncat _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
strspn, wcsspn, _mbsspn, _mbsspn_l
Security Note