liveBook · Manning

String.h

As explained in Modern C, Third Edition

string.h is the C standard header that declares functions for manipulating strings and related memory sequences.

string.h is the C standard header for manipulating character arrays, null-terminated strings, and related memory sequences. Its functions divide into two naming families: mem functions operate on a specified number of elements, while str functions expect null-terminated strings. section 6.1.5 section 8.9

The central safety and behavior rules are:

  • Use memcpy(target, source, len) for a known element count; the source and target arrays must be distinct. section 6.1.5
  • Use memcmp and memchr for bounded comparison and search; neither operation requires a null-terminated string. section 6.1.5
  • Use strlen only when the array contains a terminating zero character; it returns that character's position, not the array's capacity. section 6.1.5
  • Use strcpy only with a terminated source and a target large enough for the copied string; strdup allocates storage before copying. section 6.1.5

Copying a program name

The example computes strlen(argv[0]), allocates an array with one additional element for the terminating zero, copies the characters with memcpy, and checks the result with strcmp. The extra element is essential: the copied character count excludes the terminator. section 6.1.5

How Modern C, Third Edition covers it

How deep it goes
Defines function families, termination rules, size constraints, and a combined copying example without descending into implementation internals.
Examples
Uses argv[0], strlen, memcpy, strcmp, and a variable-length array to verify a copied program name.
What it assumes
Assumes familiarity with char arrays and pointers while making null termination and array-capacity requirements explicit.
What you can do after
Apply string.h functions to copy, compare, search, and measure terminated strings or bounded character arrays.
How it connects
Connects character arrays, null-terminated strings, memory operations, command-line arguments, variable-length arrays, and the C standard library.

Also explained in

Compare how 2 books explain string.h →

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.