Arrays and String Handling
Fixed-size arrays, and why a C string is really just a null-terminated char array.
What you'll learn
- Declare and index a fixed-size array
- Explain that a C string is a char array ending in a '\0' null terminator, not a distinct string type
- Use strlen from <string.h> to find a string's length up to its null terminator
Explanation
A C array has a fixed size baked in at declaration: int numbers[3] = {10, 20, 30};. You access elements with zero-based indexing, numbers[0] through numbers[2]. Unlike some languages' arrays, a plain C array carries no built-in length -- the program has to track how many elements are valid itself, or rely on a fixed, known size.
C has no dedicated string type. A C string is simply a char array where the end of the meaningful text is marked by a special null terminator byte, written '\0', whose value is 0. The string literal "Ada" is actually stored as 4 bytes: 'A', 'd', 'a', '\0' -- the compiler adds the terminator for you automatically for string literals. Every function that works with C strings (like printf's %s) scans forward from the given address until it finds that '\0' byte, and stops there.
This has a real consequence: a C string's "length" (the number of real characters, not counting the terminator) is not something the array itself stores -- you compute it by scanning, which is exactly what strlen from <string.h> does. strlen returns a size_t (an unsigned integer type), printed with the %zu format specifier. If a char array is never properly null-terminated, strlen (and %s) will keep reading past the end of the array into whatever memory happens to follow -- a real and common source of bugs.
Guided lab
Predict: Arrays and a null-terminated string
Read this program and predict exactly what it prints.
#include <stdio.h>
#include <string.h>
int main(void) {
char name[20] = "Ada";
int numbers[3] = {10, 20, 30};
printf("Name: %s\n", name);
printf("Length: %zu\n", strlen(name));
printf("First number: %d\n", numbers[0]);
printf("Sum: %d\n", numbers[0] + numbers[1] + numbers[2]);
return 0;
}Stuck? Get a hint.
Common mistakes
- Forgetting that a C string needs a null terminator to know where it ends -- a char array missing '\0' isn't a valid string, even if it 'looks' full of the right characters.
- Using the wrong printf specifier for strlen's return value -- it's a size_t, matched by %zu, not %d.
- Assuming a C array knows its own length at runtime -- you must track the size yourself or rely on a known fixed size.
Knowledge check
Takeaway
A C string is just a char array ending in '\0' -- functions like strlen and printf's %s work by scanning forward until they find that terminator byte.
Summary
Arrays have a fixed, untracked size; C strings are char arrays terminated by '\0', and strlen (returning a size_t) computes length by scanning for it.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.