Export (0) Print
Expand All
Expand Minimize
13 out of 22 rated this helpful - Rate this topic

strlen, wcslen, _mbslen, _mbstrlen

Get the length of a string.

size_t strlen(
   const char *string 
);
size_t wcslen(
   const wchar_t *string 
);
size_t _mbslen(
   const unsigned char *string 
);
size_t _mbstrlen(
   const char *string 
);

Parameters

string
Null-terminated string.

Return Value

Each of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error.

Remarks

Each of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise.

Security Note    These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tcslen strlen strlen wcslen
_tcsclen strlen _mbslen wcslen

_mbslen and _mbstrlen return the number of multibyte characters in a multibyte-character string. _mbslen recognizes multibyte-character sequences according to the multibyte code page currently in use; it does not test for multibyte-character validity. _mbstrlen tests for multibyte-character validity and recognizes multibyte-character sequences according to the LC_CTYPE category setting of the current locale. For more information about the LC_CTYPE category, see setlocale.

Requirements

Routine Required header Compatibility
strlen <string.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
wcslen <string.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_mbslen <mbstring.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_mbstrlen <stdlib.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_strlen.c

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

int main( void )
{
   char buffer[61] = "How long am I?";
   int  len;
   len = strlen( buffer );
   printf( "'%s' is %d characters long\n", buffer, len );
}

Output

'How long am I?' is 14 characters long

See Also

String Manipulation Routines | Locale Routines | setlocale | strcat | strcmp | strcoll Functions | strcpy | strrchr | _strset | strspn | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.