Skip to content

Latest commit

 

History

History
109 lines (84 loc) · 3.93 KB

File metadata and controls

109 lines (84 loc) · 3.93 KB
title _TRUNCATE | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-standard-libraries
ms.tgt_pltfrm
ms.topic article
f1_keywords
_TRUNCATE
TRUNCATE
dev_langs
C++
helpviewer_keywords
TRUNCATE constant
_TRUNCATE constant
ms.assetid ad093dbf-1aa5-4bd2-9268-efc68afd8434
caps.latest.revision 8
author corob-msft
ms.author corob
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

_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