You can get the length of a string using the strlen
function.
This function is declared in the header file string.h.
size_t
strlen (const char *s)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The strlen
function returns the length of the
string s in bytes. (In other words, it returns the offset of the
terminating null byte within the array.)
For example,
strlen ("hello, world") ⇒ 12
When applied to an array, the strlen
function returns
the length of the string stored there, not its allocated size. You can
get the allocated size of the array that holds a string using
the sizeof
operator:
char string[32] = "hello, world"; sizeof (string) ⇒ 32 strlen (string) ⇒ 12
But beware, this will not work unless string is the array itself, not a pointer to it. For example:
char string[32] = "hello, world";
char *ptr = string;
sizeof (string)
⇒ 32
sizeof (ptr)
⇒ 4 /* (on a machine with 4 byte pointers) */
This is an easy mistake to make when you are working with functions that take string arguments; those arguments are always pointers, not arrays.
It must also be noted that for multibyte encoded strings the return
value does not have to correspond to the number of characters in the
string. To get this value the string can be converted to wide
characters and wcslen
can be used or something like the following
code can be used:
/* The input is instring
. The length is expected inn
. */ { mbstate_t t; char *scopy = string; /* In initial state. */ memset (&t, '\0', sizeof (t)); /* Determine number of characters. */ n = mbsrtowcs (NULL, &scopy, strlen (scopy), &t); }
This is cumbersome to do so if the number of characters (as opposed to bytes) is needed often it is better to work with wide characters.
The wide character equivalent is declared in wchar.h.
size_t
wcslen (const wchar_t *ws)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
The wcslen
function is the wide character equivalent to
strlen
. The return value is the number of wide characters in the
wide string pointed to by ws (this is also the offset of
the terminating null wide character of ws).
Since there are no multi wide character sequences making up one wide character the return value is not only the offset in the array, it is also the number of wide characters.
This function was introduced in Amendment 1 to ISO C90.
size_t
strnlen (const char *s, size_t maxlen)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
This returns the offset of the first null byte in the array s,
except that it returns maxlen if the first maxlen bytes
are all non-null.
Therefore this function is equivalent to
(strlen (s) < maxlen ? strlen (s) : maxlen)
but it
is more efficient and works even if s is not null-terminated so
long as maxlen does not exceed the size of s’s array.
char string[32] = "hello, world"; strnlen (string, 32) ⇒ 12 strnlen (string, 5) ⇒ 5
This function is part of POSIX.1-2008 and later editions, but was available in the GNU C Library and other systems as an extension long before it was standardized. It is declared in string.h.
size_t
wcsnlen (const wchar_t *ws, size_t maxlen)
¶Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.
wcsnlen
is the wide character equivalent to strnlen
. The
maxlen parameter specifies the maximum number of wide characters.
This function is part of POSIX.1-2008 and later editions, and is declared in wchar.h.