Lesson 17 / 38

Strings

Work with C strings as null-terminated char arrays and see why the hidden terminator matters.

Null-terminated

A C string is a char array followed by a hidden '\0' (null character) marking its end. "Hi" actually stores 3 bytes: 'H', 'i', '\0'.

Two ways to declare

A string literal or an explicit char array — both are mutable arrays here.

char name[20] = "Alice";     // array, room to grow
char city[]  = "Delhi";      // sized to fit exactly

printf("%s lives in %s\n", name, city);

Output:

Alice lives in Delhi

%s expects null-termination

printf("%s", str) keeps printing bytes until it finds '\0'. A char array missing the null byte causes %s to read past its bounds.