Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

strlen — string length

strlen from <string.h> returns a string's length in bytes before the first '\0', excluding that terminator.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <string.h>
size_t strlen(const char *s);

strlen(s) returns a size_t value. It counts bytes, not Unicode characters or visible symbols. An empty string has length 0.

s must point to a valid null-terminated string. A null pointer or a missing terminator within the accessible array causes undefined behavior; the function takes no buffer capacity.

In the example, strlen(text) stops at the first '\0'. In contrast, sizeof text includes the entire array, including the final terminator added to the literal. Use %zu to print size_t.

Examples

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

int main(void)
{
    const char text[] = "cat\0dog";
    printf("strlen: %zu\n", strlen(text));
    printf("sizeof: %zu\n", sizeof text);
    return 0;
}

Result

strlen: 3
sizeof: 8

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources