Compartilhar via


_TRUNCATE

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at _TRUNCATE.

Specifies string truncation behavior.

Syntax

#include <stdlib.h>  

Remarks

_TRUNCATE enables truncation behavior when passed as the count parameter to these functions:

strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

mbstowcs_s, _mbstowcs_s_l

mbsrtowcs_s

wcstombs_s, _wcstombs_s_l

wcsrtombs_s

_snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l

vsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l

If the destination buffer is too small to hold the entire string, the normal behavior of these functions is to treat it as an error situation (see Parameter Validation). However, if string truncation is enabled by passing _TRUNCATE, these functions will copy only as much of the string as will fit, leaving the destination buffer null-terminated, and return successfully.

String truncation changes the return values of the affected functions. The following functions return 0 if no truncation occurs, or STRUNCATE if truncation does occur:

strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l

strncat_s, _strncat_s_l, wcsncat_s, _wcsncat_s_l, _mbsncat_s, _mbsncat_s_l

wcstombs_s, _wcstombs_s_l

mbstowcs_s, _mbstowcs_s_l

The following functions return the number of characters copied if no truncation occurs, or -1 if truncation does occur (matching the behavior of the original snprintf functions):

_snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l

vsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l

Example

// crt_truncate.c  
#include <stdlib.h>  
#include <errno.h>  
  
int main()  
{  
   char src[] = "1234567890";  
   char dst[5];  
   errno_t err = strncpy_s(dst, _countof(dst), src, _TRUNCATE);  
   if ( err == STRUNCATE )  
      printf( "truncation occurred!\n" );  
   printf( "'%s'\n", dst );  
}  
truncation occurred!  
'1234'  

See Also

Global Constants