C Reference ยท string.h
C strlen() function
Reference for C strlen(): count the characters before the terminating null byte, how it differs from sizeof, its cost, and the bounded strnlen() variant.
strlen() returns the length of a string: the number of bytes before the terminating null character '\0'. The terminator itself is not counted. It finds the end by scanning, so it takes time proportional to the length of the string.
Remember that a C string is just an array of char that ends with a null byte. strlen counts bytes, not human-visible characters, so a UTF-8 string with accented letters is longer in bytes than in characters.
#include <string.h> size_t strlen(const char *s);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
size_t strlen(const char *s); size_t strnlen(const char *s, size_t maxlen); /* POSIX.1-2008, not part of ISO C */
| Name | Description |
|---|---|
s |
Pointer to a null-terminated string. |
maxlen |
(strnlen) The most bytes to examine. |
Returns: the number of bytes before the first '\0'. strnlen returns the length, or maxlen if no terminator was found within that many bytes.
Errors / undefined behavior: s must be a valid pointer to an array that contains a null byte. A missing terminator makes strlen read past the array (undefined behavior), and a NULL argument crashes.
Usage notes
strlenversussizeof.sizeofis a compile-time size of the array (including the terminator and any unused space) and is wrong for pointers.strlenis a run-time count up to the terminator. Usesizeof bufferfor capacity andstrlenfor current length.- Cost is O(n). Do not call
strlen(s)in a loop condition whensdoes not change:for (i = 0; i < strlen(s); i++)is quadratic unless the compiler hoists the call. Store the length once. - Unsigned arithmetic. The result is
size_t.strlen(a) - strlen(b)wraps around whenbis longer, andstrlen(s) - 1is huge for an empty string. Compare, or check for zero first. - Allocating a copy: reserve
strlen(s) + 1bytes, one more for the terminator. Forgetting the+ 1is a very common off-by-one. strnlen(POSIX.1-2008) is for buffers that may be unterminated, for example fixed-size fields read from a file. Declaring it needs_POSIX_C_SOURCE 200809Lon strict modes.- Wide strings:
wcslen()in<wchar.h>countswchar_telements; for UTF-8 character counts see multibyte conversion. - Compilers know
strlen. For string literals GCC and Clang compute the length at compile time, sostrlen("hello")costs nothing. - Embedded nulls: binary data containing
0bytes is not a string. Track the length separately and usememcpy, notstrlen.
Related
strcpyandstrncpy,strcat,strcmp,sizeof,size_t- Lessons: Strings, String functions