C Strings, Character Arrays and the Null Terminator
C Strings, Character Arrays and the Null Terminator
A school name needs both visible characters and a reliable way to mark where the name ends. C strings use a zero character for that boundary.
A C string is a sequence of characters terminated by a null character, written \0. A character array is only a string when it contains that terminator within accessible storage.
Explain
Distinguish character count, storage capacity and the terminator.
Trace
Change the first character to M and observe the name and length.
Apply
Leave space for the null terminator when storing text.
Strings: follow the data
Start
A school name needs both visible characters and a reliable way to mark where the name ends. C strings use a zero character for that boundary.
Operation
A C string is a sequence of characters terminated by a null character, written \0. A character array is only a string when it contains that terminator within accessible storage.
Result
Name: Asha Length: 4; capacity: 5
Understand the operation
char name[] = "Asha" creates five elements: four letters and one null terminator. strlen counts characters before the terminator and returns size_t; sizeof name counts all five bytes of this array.
Use %s only with a valid terminated string. A string literal must not be modified; use a writable initialized character array when changes are required. This lesson counts basic execution characters, not general Unicode user-perceived characters.
Read the complete C program
This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.
C17 · complete program
#include <stdio.h>
#include <string.h>
int main(void) {
char name[] = "Asha";
printf("Name: %s\n", name);
printf("Length: %zu; capacity: %zu\n", strlen(name), sizeof name);
return 0;
}
Expected output
Name: Asha
Length: 4; capacity: 5
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
int main(void) {
char name[] = "Asha";
printf("Name: %s\n", name);
printf("Length: %zu; capacity: %zu\n", strlen(name), sizeof name);
return 0;
}
The initializer includes the terminator automatically.
strlen stops before that terminator, while sizeof measures the full array storage. These quantities answer different questions.
Follow the worked calculation
Read the starting values
The initializer includes the terminator automatically.
Perform the operation
strlen stops before that terminator, while sizeof measures the full array storage. These quantities answer different questions.
Check the observable result
Name: Asha Length: 4; capacity: 5
Name: Asha Length: 4; capacity: 5
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <string.h>
int main(void) {
char name[] = "Asha";
printf("Name: %s\n", name);
printf("Length: %zu; capacity: %zu\n", strlen(name), sizeof name);
return 0;
}
Change the first character to M and observe the name and length.
Worked answer
The array is writable and the replacement preserves its terminator. One letter changes, but the number of visible basic characters remains four.
C17 · complete program
#include <stdio.h>
#include <string.h>
int main(void) {
char name[] = "Asha";
name[0] = 'M';
printf("Name: %s; length: %zu\n", name, strlen(name));
return 0;
}
Expected output
Name: Msha; length: 4
Run in C compiler →Recognize the important boundaries
A destination for copied or concatenated text must include space for the final terminator. strcpy and strcat do not know the destination capacity.
strlen cannot safely discover the length of an arbitrary unterminated array. Pass a known length for raw character data or validate termination within the available bound.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A C string is a sequence of characters terminated by a null character, written \0. A character array is only a string when it contains that terminator within accessible storage.
Continue with reading bounded text.
Continue with manual length counting.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.