Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <string.h>

size_t strlen(const char *s);

Try it

C strlen versus sizeof

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.

C Common uses and a classic bug

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

C
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

  • strlen versus sizeof. sizeof is a compile-time size of the array (including the terminator and any unused space) and is wrong for pointers. strlen is a run-time count up to the terminator. Use sizeof buffer for capacity and strlen for current length.
  • Cost is O(n). Do not call strlen(s) in a loop condition when s does 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 when b is longer, and strlen(s) - 1 is huge for an empty string. Compare, or check for zero first.
  • Allocating a copy: reserve strlen(s) + 1 bytes, one more for the terminator. Forgetting the + 1 is 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 200809L on strict modes.
  • Wide strings: wcslen() in <wchar.h> counts wchar_t elements; for UTF-8 character counts see multibyte conversion.
  • Compilers know strlen. For string literals GCC and Clang compute the length at compile time, so strlen("hello") costs nothing.
  • Embedded nulls: binary data containing 0 bytes is not a string. Track the length separately and use memcpy, not strlen.