liveBook · Manning

Strlen

As explained in Modern C, Third Edition

strlen is a C standard-library function that returns the number of characters before the terminating zero in a string.

strlen returns the number of characters before the first zero character in a null-terminated C string; it does not report the size of the containing array. section 6.1.5

Use strlen safely by observing these rules:

  • Pass an argument that is genuinely zero-terminated; ensuring this is the caller’s responsibility. section 6.1.5

Measuring a program name

A program can compute strlen(argv[0]), allocate a character array of len + 1, and copy the name’s len characters into it. The extra element accommodates the terminating zero.

How Modern C, Third Edition covers it

How deep it goes
Defines the first-zero rule, array-size distinction, termination requirement, and size_t result without detailing implementation internals.
Examples
Uses argv[0] to size a destination array before copying and comparing the program name.
What it assumes
Assumes familiarity with character arrays and teaches that C strings require a terminating zero character.
What you can do after
Apply strlen through the C string library while preserving zero termination and allocating room for the terminator.
How it connects
Connects strlen with strings, character arrays, memcpy, strcpy, 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, Third Edition. The section links point to the parts of the book it draws on.