The Library, and Its Terms of Service

Walking to the terminator by hand taught you what strings are; <string.h> packages the walks you will do daily. Four functions carry most of the load, strlen, strcpy, strcat, strcmp, and every one of them operates on the same terms: you guarantee the terminators exist and the destinations are big enough, and they do the walking. Exams then ask you to write each one yourself, which this lesson also delivers.

strlen: The Walk, Packaged

strlen(s) returns the length, the character count before the terminator. Its return type is size_t, an unsigned type; in this dialect, store it in an unsigned long and print with %lu, the same discipline as sizeof in chapter 2.

Length 10, size 11: the lesson-one distinction, now measured by the library. The hand-written version is last lesson's loop, and exams ask for it by name:

length = 0;
while (s[length] != '\0') {
    length++;
}

strcpy and strcat: Copying Demands Capacity

strcpy(dest, src) copies src's characters and its terminator into dest. strcat(dest, src) first walks to dest's terminator, then copies src from there, joining the strings. Neither knows dest's size; both simply write. The contract is therefore yours to honor: dest must have room for the combined length plus one terminator.

The arithmetic before the call is the safety analysis: 5 + 1 + 6 = 12 characters plus a terminator, 13 of 32 bytes, fine. Copy into too small a destination and you have written last lesson's buffer overflow with library assistance; = does not work on arrays (full = first does not compile), which is why strcpy exists at all, another standard exam question.

strcmp: Three Answers, Not Two

strcmp(a, b) compares character by character and returns negative when a sorts before b, zero when they are equal, and positive when a sorts after b. Not -1, 0, 1: any negative or positive value, so the tests are < 0, == 0, > 0.

Two traps live here. First, if (a == b) on arrays compares addresses, not contents, a comparison that says "are these the same array", almost never the question; string equality is strcmp(a, b) == 0. Second, because strcmp returns 0 for equal, if (strcmp(a, b)) reads as "if different", inverted from intuition; write the explicit == 0. The comparison order is character codes, so all uppercase sorts before all lowercase in ASCII, and the exam name for the whole scheme is lexicographic order.

Writing Them Yourself

The hand-written versions are one loop each, and assembling them is the exam's favorite long-form question. Copy: walk src, assigning into dest, then place the terminator. Compare: walk both while characters match, then return the difference at the first mismatch. Each is the null-terminator convention plus one idea, and having built strings by hand, you already own the parts. The exercise has you compose the library versions; write the manual versions on paper once and keep them.

What Else Is in the Box

<string.h> holds more that the course will meet in context. strncpy and strncat take explicit length caps but carry famous quirks (a full strncpy leaves dest unterminated, so it is not the automatic safe choice its name suggests). strncmp(a, b, n) compares at most n characters and returns the same negative, zero, or positive as strcmp, which makes it the tool for testing a prefix. strchr(s, c) finds the first occurrence of a character and strrchr(s, c) the last, the extra r standing for reverse; strstr(s, sub) searches for a whole substring instead. memcpy moves raw bytes with no interest in terminators at all. For now the four core functions, used within their contracts, cover the chapter's programs.

Key Takeaways

  • strlen returns the pre-terminator count as size_t; store in unsigned long, print %lu; the hand-written walk remains examinable.
  • strcpy copies including the terminator; strcat appends at dest's terminator; both trust you that dest has combined length plus one byte, and arrays cannot be assigned with =.
  • strcmp returns negative/zero/positive for before/equal/after: test with < 0, == 0, > 0, never expect exactly -1 or 1.
  • a == b on strings compares addresses; equality is strcmp(a, b) == 0, and bare strcmp in a condition means "different".
  • strncpy's no-terminator-when-full quirk means it is not automatically safer; the course's tools are bounded reads plus capacity arithmetic.