Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 06 / 21 · DATA AND FUNCTIONS

Strings and character data.

Read null termination, buffer capacity, text comparison, and encoding boundaries.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

A C string is a sequence of char values ending with a zero byte, written '\0'. The terminator is part of the storage requirement but not part of the text's length as reported by strlen. An array capable of holding five visible one-byte letters needs at least six char elements. A function that scans for the terminator assumes it exists within accessible memory; if it does not, the function can read beyond the object.

A string literal such as "hello" provides characters and a terminating zero. Its contents must not be modified. A writable char array initialized from that literal is a separate object whose elements may change. Comparing two char pointers with == compares pointer values, not the sequences of characters they address. strcmp compares string contents under its defined ordering rules.

02 / THE RULES

What C guarantees.

The standard string functions operate on bytes and terminators, not on user-perceived characters. In a UTF-8 environment, a visible character may occupy several bytes, so strlen is a byte count. Character classification functions also need a valid unsigned-char value or EOF; passing a negative plain char can violate their contract. Text handling therefore requires an explicit encoding assumption.

A bounded write must reserve space for the terminator. snprintf reports how many characters would have been written, allowing code to detect truncation. Size information should travel with a buffer, just as an array length travels with a pointer. The example forms a short label in a fixed-size array and checks whether the formatted result fit before treating it as complete.

03 / UNDER THE SURFACE

Why the detail matters.

A null terminator is a sentinel, not a separate length stored alongside a pointer. Finding it requires scanning bytes until zero appears, which is why a missing terminator is dangerous. A capacity-aware interface keeps the maximum accessible byte count available. Functions such as snprintf take that capacity and can report truncation; functions that know only a char * cannot discover the allocation size from the pointer alone. Treat every string construction as a capacity calculation including the final zero byte.

The char type represents a byte-sized unit of the execution character set, not necessarily a complete written symbol. Even when a terminal uses UTF-8, standard string functions measure bytes. A byte-wise operation may split a multibyte sequence and produce invalid text. For ASCII-like labels this may be acceptable; for international names it needs a deliberate text model. The language's low-level representation and the user's idea of a character are different layers.

04 / CODE, EXPLAINED

See the rule in context.

strings-and-character-data.c
#include <stdio.h>

int main(void) {
    char label[32];
    int written = snprintf(label, sizeof label, "Item %d", 12);
    if (written < 0 || (size_t)written >= sizeof label) {
        fputs("Label did not fit\n", stderr);
        return 1;
    }
    printf("%s\n", label);
    return 0;
}
FOLLOW THE EXECUTION

label has room for 32 bytes. snprintf writes Item 12 followed by a zero terminator and returns 7, the number of characters that would be written before the terminator. Seven is below the capacity, so the program prints the completed string. The cast in the comparison occurs only after the negative error result has been checked.

05 / THE BOUNDARY

Where the rule stops.

A buffer capacity is measured in bytes, not visual characters. snprintf prevents overflow when used correctly, but truncation still changes meaning and must be handled when a complete label is required.

06 / THE CONNECTION

Keep this model.

Strings are passed to functions as pointers to their first characters. The next chapter explains function declarations and parameter passing more generally, including why a function can modify an array through a pointer but cannot replace the caller's scalar argument by assigning its local parameter.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.