Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 7
Strings in C - The Null Terminator Contract
In C, a string is a `char` array with a **null terminator** (`null terminator (\x00)`, ASCII 0) at the end. EVERY standard library string function depends on this terminator - they scan until they find `null terminator (\x00)`. If you create a `char` array and forget the terminator, every `strlen`, `printf("%s")`, and `strcpy` call on it is undefined behaviour that reads into random memory.
Strings - Null Terminator, Literals & Manual Construction
Strings - Null Terminator, Literals & Manual Construction
#include <stdio.h>
#include <string.h>
int main(void) {
/* ── String literal - stored in READ-ONLY memory ─────── */
const char *lit = "hello";
/* Memory: ['h','e','l','l','o','