liveBook · Manning

Strlen

As explained in Modern C

strlen is a C standard library function that returns the number of characters before the null terminator of a string.

strlen returns the number of characters before a string’s first 0 character. It counts the string, not the entire char array, and requires a 0-terminated argument. section 6.1.5

Counting before copying

size_t const len = strlen(argv[0]); char name[len + 1]; memcpy(name, argv[0], len); name[len] = 0; Here, strlen supplies the character count; the extra array element stores the terminating 0 before the copied value is compared with strcmp. section 6.1.5

Keep these constraints visible:

  • A string must contain a terminating 0 character; using strlen on a non-string has undefined behavior. section 6.1.5

How Modern C covers it

How deep it goes
Defines the count and demonstrates allocation and copying, while deferring implementation internals.
Examples
Demonstrates measuring argv[0], sizing a variable-length array, copying characters, and adding the terminating 0.
What it assumes
Requires familiarity with char arrays and teaches that C strings require an explicit terminating 0 character.
What you can do after
Apply via the C standard library to measure valid strings and size storage for subsequent operations.
How it connects
Connects strlen with C strings, char arrays, null termination, memcpy, and strcmp.

Also explained in

Compare how 2 books explain strlen →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.