The Null Terminator
Part of the Logic & Flow section of Coddy's C journey — lesson 13 of 63.
You've learned that strings in C are character arrays, but there's a crucial detail that makes them work properly: every C string must end with a special character called the null terminator, written as \0.
The null terminator is a character with the ASCII value of zero that marks the end of a string. Without it, functions like printf wouldn't know where your string ends and would continue reading memory until they randomly encounter a zero byte, often producing garbage output.
char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
// The \0 tells C: "The string ends here"When you initialize a string with a literal like "Hello", C automatically adds the null terminator for you. But when you manually build a string character by character, you must remember to add \0 at the end.
This null terminator is why a 5-character word like "Hello" needs a 6-element character array - one extra space for the \0. Understanding this concept is essential because it's the foundation of how all C string functions determine where strings begin and end.
Challenge
EasyWrite a C program that demonstrates creating and working with strings using manual character-by-character initialization with proper null termination. Your program should:
- Declare a character array named
namewith exactly 5 elements - Manually initialize the
namearray character by character to spell "John" with proper null termination:- First element: 'J'
- Second element: 'o'
- Third element: 'h'
- Fourth element: 'n'
- Fifth element:
'\0'(null terminator)
- Declare a second character array named
citywith exactly 7 elements - Manually initialize the
cityarray character by character to spell "Boston" with proper null termination:- Use individual character assignments for 'B', 'o', 's', 't', 'o', 'n', and
'\0'
- Use individual character assignments for 'B', 'o', 's', 't', 'o', 'n', and
- Print both strings using
printfwith the%sformat specifier - Demonstrate what happens without null termination by creating a third character array named
testwith 4 elements - Initialize
testwith only the characters 'A', 'B', 'C', 'D' (no null terminator) - Print the
testarray using%sto show unpredictable output
Your output should display the results in the following format:
Name: John
City: Boston
Test without null terminator: ABCD[unpredictable characters]This challenge tests your understanding of manual string construction, the critical importance of the null terminator '\0' in C strings, and how printf with %s relies on finding the null terminator to know where the string ends.
Cheat sheet
C strings are character arrays that must end with a null terminator \0 to mark the end of the string.
The null terminator has ASCII value zero and tells functions like printf where the string ends:
char word[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
// The \0 tells C: "The string ends here"When using string literals like "Hello", C automatically adds the null terminator. When building strings character by character, you must manually add \0.
A string needs one extra array element for the null terminator - "Hello" (5 characters) requires a 6-element array.
Try it yourself
#include <stdio.h>
int main() {
// TODO: Write your code here
// Declare and initialize the character arrays as specified
// Print the results
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions