C-Strings as Null-Terminated Character Arrays
Why the '\0' byte is the sentinel for every string algorithm
C has no built-in "string" primitive type. A string in C is simply an array of char bytes ending with a special sentinel byte called the Null Terminator ('\0', ASCII 0).
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Character | 'A' | 'p' | 'p' | 'l' | 'e' | '\0' (Sentinel) |
| ASCII | 65 | 112 | 112 | 108 | 101 | 0 |
⚠️
Remember Array Sizing for Null Terminator
To store an N-character word, the array size MUST be at least N + 1 bytes to accommodate the trailing '\0'. Declaring char s[5] = "Apple"; is an error because it lacks room for '\0'!