Menu
Coddy logo textTech

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 icon

Challenge

Easy

Write a C program that demonstrates creating and working with strings using manual character-by-character initialization with proper null termination. Your program should:

  1. Declare a character array named name with exactly 5 elements
  2. Manually initialize the name array 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)
  3. Declare a second character array named city with exactly 7 elements
  4. Manually initialize the city array character by character to spell "Boston" with proper null termination:
    • Use individual character assignments for 'B', 'o', 's', 't', 'o', 'n', and '\0'
  5. Print both strings using printf with the %s format specifier
  6. Demonstrate what happens without null termination by creating a third character array named test with 4 elements
  7. Initialize test with only the characters 'A', 'B', 'C', 'D' (no null terminator)
  8. Print the test array using %s to 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow