#include <string.h> size_t strlen(const char* s);
strlen measures the length of a null-terminated string.
A common error when using
strlen is to assume that the reported length includes the terminating null character. It does
not, so it is necessary to add one if that is the value wanted.
The length returned by
strlen is measured in bytes, not characters. This is an important distinction when using a multi-byte character encoding such as UTF-8.
This is not to say that the use of
strlen is always wrong under those circumstances: if the requirement is to measure the amount of memory occupied by the string then
strlen is a perfectly reasonable way to obtain that information. However, any processing that interprets the string as a sequence of characters should take account of the character encoding, and if the encoding uses anything other than a fixed one byte per character then
strlen will not give the appropriate result.
- ✔ C90
- ✔ C99
- ✔ C++98
- ✔ C++11
In C++, use of the header
<string.h> is deprecated in favour of