Lesson 15: Strings — a char array & '\0'
After learning arrays, it's time for a secret: C has no special 'string' type. A string is simply an ordered array of chars that ends with the special character '\0' — the null terminator. That's why char name[] = "Dana"; occupies 5 cells: four letters plus one cell for the end marker. We'll learn t
A string is like a train of letter-cars, and at the end there is always a quiet 'end' car attached — the '\0'. Anyone walking along the train knows to stop exactly when they meet the end car.
- string (char array)
- A sequence of characters stored in a char array, always ending with '\0'.
- '\0' — the null terminator
- A special character with value 0 that marks the end of the string. Without it no function knows where to stop.
- strlen(s)
- Returns the string's length — the number of characters up to the '\0', not counting it.
- strcpy(dest, src)
- Copies src into dest, including the '\0'. You must make sure dest is large enough.
- strcmp(a, b)
- Compares two strings and returns 0 precisely when they are equal — a classic trap in if conditions.