C Workshop · Strings
Foundation C

Strings

A C string is a char array that ends with a hidden '\0' byte. Functions like strlen stop at that terminator.

What it is

In C a string is not a special object. It is a row of characters that ends with a hidden 0 byte called the null terminator. Functions like strlen walk until they find that 0.

Approach

  1. Store characters in order.
  2. Keep a '\0' at the end.
  3. Use string.h helpers, or walk until s[i] == 0.

Where this is used in the real world

Diagram

1

K

[0]

2

o

[1]

3

d i n

[2..4]

4

\0

stop

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2#include <string.h>
3int main(void) {
4    char s[16] = "Kodin";
5    printf("%zu\n", strlen(s));
6    return 0;
7}
What this line does
Press Run algorithm. The highlight will move through the C code.
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. "Kodin" needs at least how many chars of storage?

Terminator.

2. strlen counts:

Stops at 0.

3. A C string is:

Convention + array.

4. string.h is included for:

String helpers.

5. Missing \0 and then strlen is:

Read until a random 0.