5.1.20 · D1C Programming

Foundations — String handling — char arrays, null terminator, strcpy, strcat, strlen, sprintf (dangers)

1,929 words9 min readBack to topic

Before you can read strlen, strcpy, or sprintf in the parent note, every tiny symbol on those lines must first mean something. This page builds each one from absolute zero, in the order they lean on each other.


1. A char — one box that holds one small number

The letter 'A' is not really stored as a shape. It is stored as the number . The quotes in 'A' are C's way of saying "give me the ASCII number for this letter" so you don't have to memorise that 'A' is .

Figure — String handling — char arrays, null terminator, strcpy, strcat, strlen, sprintf (dangers)

Reveal check:

'0' (the digit) versus '\0' (the terminator) — same or different?
Totally different: '0' is the number 48, '\0' is the number 0.

2. An array — many boxes in a row, side by side

The number in s[i] is the index — how many boxes to walk from the start. It begins at : the first box is s[0], not s[1]. So a 6-box array has valid indices s[0] through s[5] — the last index is always size minus one.

Figure — String handling — char arrays, null terminator, strcpy, strcat, strlen, sprintf (dangers)

Reveal check:

In char s[6], what is the highest legal index?
s[5] (size − 1).

3. The null terminator '\0' — the STOP box

A row of boxes has no built-in sign telling you where the word stops. So C plants a STOP box right after the last real letter. Every string function's job is: walk forward until you meet the STOP.

Figure — String handling — char arrays, null terminator, strcpy, strcat, strlen, sprintf (dangers)

Reveal check:

Why can't a program tell where a string ends without '\0'?
A bare row of char boxes stores no length; the '\0' is the only end marker.

4. "..." versus '...' — a whole word versus one letter

So "Hi" silently occupies 3 boxes even though you typed 2 letters. This hidden extra box is the +1 you keep hearing about.

Reveal check:

How many boxes does the literal "cat" need?
4 — three letters plus one '\0'.

5. A pointer char * — an address, not the boxes themselves

Crucially, a pointer holds only the starting address. It does not remember how many boxes follow. That is exactly why C needs the '\0' — the pointer alone can't tell you where to stop. Deep dive: Pointers in C.

Reveal check:

Does a char * remember how many characters follow its address?
No — only the start; the '\0' marks the end instead.

6. size_t and the counter i, n — indices that never go negative

Reveal check:

Why use size_t and not int for a length?
A length or index is never negative; size_t is the unsigned counting type.

7. !=, =, and the while loop — the "walk until STOP" engine

The parent's core line is while (s[n] != '\0') n++; — read it as: "while the box I'm looking at is not the STOP box, count it and step to the next." This is the shape of every string routine.

Reveal check:

What is the difference between = and !=?
= stores a value; != asks "are these unequal?".

8. — "cost grows with the length"

This is why calling strlen (or strcat, which secretly scans to find the end) inside a loop can become slow: each call re-walks the whole row.

Reveal check:

Why is strlen and not instant?
With no stored length it must scan box by box until '\0' — that is steps.

9. Buffer overflow — writing into a box you don't own

The classic cause is forgetting the STOP box's +1: char buf[5]; strcpy(buf, "Hello"); needs 6 boxes into 5.

Reveal check:

What happens in C if you write one box past an array's end?
Undefined behaviour — no bounds check; may corrupt memory, crash, or be exploited.

How these foundations feed the topic

char = one byte number

array = boxes in a row

null terminator zero byte

string literal adds STOP for free

pointer holds only the start address

sizeof lies on a pointer

while loop walks until STOP

size_t counter

strlen strcpy strcat

Big-O n scanning cost

buffer overflow if no room

String Handling topic


Equipment checklist

Do you already have each piece? Reveal only after you answer aloud.

A char really stores what?
One byte — a number interpreted as a letter via ASCII.
The first element of char s[6] is written how, and the last?
s[0] first, s[5] last (index = size − 1).
What is '\0' and its numeric value?
The null terminator, a char holding the number .
How many boxes does "cat" occupy?
4 — three letters plus the '\0'.
Difference between '0' and '\0'?
'0' is (a digit); '\0' is (the terminator).
What does a char * store, and what does it NOT store?
It stores the start address; it does NOT store the length.
Why can sizeof buf be wrong?
If buf is a pointer, sizeof gives pointer size, not the buffer's size.
Read while (s[n] != '\0') n++; in plain words.
While this box is not the STOP box, count it and step forward.
Why is strlen ?
No stored length, so it scans every box up to '\0'.
What is a buffer overflow in one line?
Writing past the last legal box of an array — undefined behaviour in C.