Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 7

Strings in C - The Null Terminator Contract

In C, a string is a `char` array with a **null terminator** (`null terminator (\x00)`, ASCII 0) at the end. EVERY standard library string function depends on this terminator - they scan until they find `null terminator (\x00)`. If you create a `char` array and forget the terminator, every `strlen`, `printf("%s")`, and `strcpy` call on it is undefined behaviour that reads into random memory.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Strings - Null Terminator, Literals & Manual Construction

Strings - Null Terminator, Literals & Manual Construction
#include <stdio.h>
#include <string.h>

int main(void) {
    /* ── String literal - stored in READ-ONLY memory ─────── */
    const char *lit = "hello";
    /* Memory: ['h','e','l','l','o','']
       lit points to the first 'h'.
       lit[5] = 'X';  ← undefined behaviour: modifying read-only data */

    /* ── char array - writable copy on the stack ─────────── */
    char greeting[] = "hello";    /* compiler adds  automatically */
    /* Memory: ['h','e','l','l','o',''] - 6 bytes total    */
    greeting[0] = 'H';            /* OK: writable memory */
    printf("%s
", greeting);     /* Hello */

    /* ── Manual array with explicit null terminator ─────── */
    char name[10];
    name[0] = 'A'; name[1] = 'l'; name[2] = 'i';
    name[3] = 'c'; name[4] = 'e'; name[5] = '';   /* REQUIRED */
    printf("Name: %s (len=%zu)
", name, strlen(name));  /* Alice (5) */

    /* ── sizeof vs strlen ────────────────────────────────── */
    char buf[] = "hello";
    printf("sizeof(buf)  = %zu
", sizeof(buf));   /* 6 (includes ) */
    printf("strlen(buf)  = %zu
", strlen(buf));   /* 5 (excludes ) */

    /* ── String comparison - why == doesn't work ────────── */
    char s1[] = "cat";
    char s2[] = "cat";
    /* s1 == s2 compares ADDRESSES (both point to different arrays)
       → 0 (false) even though the content is equal!           */
    if (strcmp(s1, s2) == 0) printf("strings are equal
");  /* correct */

    /* ── Reading: fgets is safer than scanf %s ─────────── */
    /* fgets(name, sizeof(name), stdin);
       Reads up to sizeof(name)-1 chars + null-terminates.    */

    return 0;
}

Common Mistakes

  • Forgetting the null terminator when building strings manually - `char s[5] = {'h','e','l','l','o'};` has no `null terminator (\x00)`. `strlen(s)` reads past the array into undefined memory.
  • Comparing strings with `==` - `s1 == s2` compares pointer addresses, not string content. Always use `strcmp(s1, s2) == 0` for equality.
  • Modifying a string literal - `char *s = "hello"; s[0] = 'H';` is undefined behaviour. String literals live in read-only memory. Use `char s[] = "hello";` for a writable copy.

Tip

Tip

Practice Strings in C The Null Terminator Contract in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

A C string is simply a sequence of characters terminated by a null byte ''

Practice Task

Note

Practice Task - (1) Write a working example of Strings in C The Null Terminator Contract from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Strings in C The Null Terminator Contract is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • In C, a string is a `char` array with a **null terminator** (`null terminator (\x00)`, ASCII 0) at the end.
  • Forgetting the null terminator when building strings manually - `char s[5] = {'h','e','l','l','o'};` has no `null terminator (\x00)`. `strlen(s)` reads past the array into undefined memory.
  • Comparing strings with `==` - `s1 == s2` compares pointer addresses, not string content. Always use `strcmp(s1, s2) == 0` for equality.
  • Modifying a string literal - `char *s = "hello"; s[0] = 'H';` is undefined behaviour. String literals live in read-only memory. Use `char s[] = "hello";` for a writable copy.

Topics in This Module

0/7 done