strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l (2024)

  • Article

Copy characters of one string to another. More secure versions of these functions are available; see strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l.

Important

_mbsncpy and _mbsncpy_l cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported in Universal Windows Platform apps.

Syntax

char *strncpy( char *strDest, const char *strSource, size_t count);char *_strncpy_l( char *strDest, const char *strSource, size_t count, _locale_t locale);wchar_t *wcsncpy( wchar_t *strDest, const wchar_t *strSource, size_t count);wchar_t *_wcsncpy_l( wchar_t *strDest, const wchar_t *strSource, size_t count, _locale_t locale);unsigned char *_mbsncpy( unsigned char *strDest, const unsigned char *strSource, size_t count);unsigned char *_mbsncpy_l( unsigned char *strDest, const unsigned char *strSource, size_t count, _locale_t locale);template <size_t size>char *strncpy( char (&strDest)[size], const char *strSource, size_t count); // C++ onlytemplate <size_t size>char *_strncpy_l( char (&strDest)[size], const char *strSource, size_t count, _locale_t locale); // C++ onlytemplate <size_t size>wchar_t *wcsncpy( wchar_t (&strDest)[size], const wchar_t *strSource, size_t count); // C++ onlytemplate <size_t size>wchar_t *_wcsncpy_l( wchar_t (&strDest)[size], const wchar_t *strSource, size_t count, _locale_t locale); // C++ onlytemplate <size_t size>unsigned char *_mbsncpy( unsigned char (&strDest)[size], const unsigned char *strSource, size_t count); // C++ onlytemplate <size_t size>unsigned char *_mbsncpy_l( unsigned char (&strDest)[size], const unsigned char *strSource, size_t count, _locale_t locale); // C++ only

Parameters

strDest
Destination string.

strSource
Source string.

count
Number of characters to be copied.

locale
Locale to use.

Return value

Returns strDest. No return value is reserved to indicate an error.

Remarks

The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character isn't appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

Important

strncpy does not check for sufficient space in strDest; this makes it a potential cause of buffer overruns. The count argument limits the number of characters copied; it is not a limit on the size of strDest. See the following example. For more information, see Avoiding buffer overruns.

If strDest or strSource is a NULL pointer, or if count is less than or equal to zero, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

wcsncpy and _mbsncpy are wide-character and multibyte-character versions of strncpy. The arguments and return value of wcsncpy and _mbsncpy vary accordingly. These six functions behave identically otherwise.

The versions of these functions with the _l suffix are identical except that they use the locale passed in instead of the current locale for their locale-dependent behavior. For more information, see Locale.

In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. For more information, see Secure template overloads.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mappings

TCHAR.H routine_UNICODE and _MBCS not defined_MBCS defined_UNICODE defined
_tcsncpystrncpy_mbsnbcpywcsncpy
_tcsncpy_l_strncpy_l_mbsnbcpy_l_wcsncpy_l

Note

_strncpy_l and _wcsncpy_l have no locale dependence; they are provided just for _tcsncpy_l and are not intended to be called directly.

Requirements

RoutineRequired header
strncpy<string.h>
wcsncpy<string.h> or <wchar.h>
_mbsncpy, _mbsncpy_l<mbstring.h>

For more platform compatibility information, see Compatibility.

Example

The following example demonstrates the use of strncpy and how it can be misused to cause program bugs and security issues. The compiler generates a warning for each call to strncpy similar to crt_strncpy_x86.c(15) : warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

// crt_strncpy_x86.c// Use this command in an x86 developer command prompt to compile:// cl /TC /W3 crt_strncpy_x86.c#include <stdio.h>#include <string.h>int main() { char t[20]; char s[20]; char *p = 0, *q = 0; strcpy_s(s, sizeof(s), "AA BB CC"); // Note: strncpy is deprecated; consider using strncpy_s instead strncpy(s, "aa", 2); // "aa BB CC" C4996 strncpy(s + 3, "bb", 2); // "aa bb CC" C4996 strncpy(s, "ZZ", 3); // "ZZ", C4996 // count greater than strSource, null added printf("%s\n", s); strcpy_s(s, sizeof(s), "AA BB CC"); p = strstr(s, "BB"); q = strstr(s, "CC"); strncpy(s, "aa", p - s - 1); // "aa BB CC" C4996 strncpy(p, "bb", q - p - 1); // "aa bb CC" C4996 strncpy(q, "cc", q - s); // "aa bb cc" C4996 strncpy(q, "dd", strlen(q)); // "aa bb dd" C4996 printf("%s\n", s); // some problems with strncpy strcpy_s(s, sizeof(s), "test"); strncpy(t, "this is a very long string", 20 ); // C4996 // Danger: at this point, t has no terminating null, // so the printf continues until it runs into one. // In this case, it will print "this is a very long test" printf("%s\n", t); strcpy_s(t, sizeof(t), "dogs like cats"); printf("%s\n", t); strncpy(t + 10, "to chase cars.", 14); // C4996 printf("%s\n", t); // strncpy has caused a buffer overrun and corrupted string s printf("Buffer overrun: s = '%s' (should be 'test')\n", s); // Since the stack grows from higher to lower addresses, buffer // overruns can corrupt function return addresses on the stack, // which can be exploited to run arbitrary code.}

Output

ZZaa bb ddthis is a very long testdogs like catsdogs like to chase cars.Buffer overrun: s = 'ars.' (should be 'test')

The layout of automatic variables and the level of error detection and code protection can vary with changed compiler settings. This example may have different results when built in other compilation environments or with other compiler options.

See also

String manipulation
Locale
Interpretation of multibyte-character sequences
_mbsnbcpy, _mbsnbcpy_l
strcat, wcscat, _mbscat
strcmp, wcscmp, _mbscmp
strcpy, wcscpy, _mbscpy
strncat, _strncat_l, wcsncat, _wcsncat_l, _mbsncat, _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn, wcsspn, _mbsspn, _mbsspn_l
strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l
strcpy_s, wcscpy_s, _mbscpy_s

strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l (2024)
Top Articles
Jeanette McCurdy's Hawaii Getaway: Exclusive TMZ Coverage
Television Archive News Search Service
Xre-02022
My Arkansas Copa
Parke County Chatter
Federal Fusion 308 165 Grain Ballistics Chart
Kansas Craigslist Free Stuff
Find All Subdomains
Sportsman Warehouse Cda
Bloxburg Image Ids
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
Oxford House Peoria Il
Facebook Marketplace Charlottesville
Socket Exception Dunkin
Fairy Liquid Near Me
Classic Lotto Payout Calculator
The ULTIMATE 2023 Sedona Vortex Guide
Craftology East Peoria Il
Www Craigslist Milwaukee Wi
Keurig Refillable Pods Walmart
Scout Shop Massapequa
Ups Print Store Near Me
Ezel Detailing
[PDF] NAVY RESERVE PERSONNEL MANUAL - Free Download PDF
Powerschool Mcvsd
Amelia Chase Bank Murder
1145 Barnett Drive
Roanoke Skipthegames Com
Catchvideo Chrome Extension
Taylored Services Hardeeville Sc
Stubhub Elton John Dodger Stadium
Unity Webgl Player Drift Hunters
Zero Sievert Coop
Reborn Rich Ep 12 Eng Sub
Wsbtv Fish And Game Report
Enjoy4Fun Uno
Ashoke K Maitra. Adviser to CMD&#39;s. Received Lifetime Achievement Award in HRD on LinkedIn: #hr #hrd #coaching #mentoring #career #jobs #mba #mbafreshers #sales…
Kerry Cassidy Portal
Wunderground Orlando
Ezpawn Online Payment
manhattan cars & trucks - by owner - craigslist
Kb Home The Overlook At Medio Creek
Kenner And Stevens Funeral Home
Denise Monello Obituary
This Doctor Was Vilified After Contracting Ebola. Now He Sees History Repeating Itself With Coronavirus
Maplestar Kemono
Beds From Rent-A-Center
1Tamilmv.kids
Wvu Workday
Compete My Workforce
What Responsibilities Are Listed In Duties 2 3 And 4
Selly Medaline
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 5700

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.