A string is a character array that ends with a null terminator (\0). A plain character array is just data without this stop sign, making it dangerous to use for text.
Imagine a road with no stop sign. A string has a stop sign (\0) at the end. A character array is just a road — the computer doesn't know where it ends and might crash into the next street!
Where does it end? Computer doesn't know!
Stops exactly here (\0).
1// Character Array (Dangerous!)2char arr[] = {'H', 'E', 'L', 'L', 'O'};3printf("%s", arr); // Might print: "HELLO??garbage??"45// String (Safe)6char str[] = "HELLO";7// Implicitly: {'H', 'E', 'L', 'L', 'O', '\0'}8printf("%s", str); // Prints: "HELLO" (stops at \0)910// Why strings are better for text:11// - strlen() works correctly12// - strcpy() knows where to stop13// - No buffer overflow bugs
Let's understand the critical difference.
Character Array — The Dangerous Road
A character array is just a collection of characters in memory:
char arr[] = {'H', 'E', 'L', 'L', 'O'};
No stop sign. No terminator. When you try to print this or find its length, the computer keeps reading past "HELLO" until it finds a random \0 somewhere in memory (or crashes).
String — The Safe Road
A string is a character array WITH a null terminator:
char str[] = "HELLO";
// Actually: {'H', 'E', 'L', 'L', 'O', '\0'}
The \0 acts like a stop sign. Functions know exactly where to stop.
The Printing Problem
When you print a character array without \0:
When you print a string with \0:
Why Strings vs Arrays Matter
This distinction prevents bugs! Without \0, functions like printf() or strlen() don't know where your data ends and will read garbage memory. Understanding this saves you from buffer overflow bugs.
"What's the difference between a character array and a string?"
A string is a character array that ends with a null terminator (\0). This terminator tells functions where the string ends, preventing them from reading past the data.