Coding/Learn/c/c strings and character manipulation
C Foundational Course
01.Introduction, Compilation Pipeline & Syntax
02.Variables, Primitives & Explicit Type Casting
03.Operators, Precedence & Booleans (<stdbool.h>)
04.Control Flow: If-Else, Ternary & Switch
05.Loops (For, While, Do-While) & Jump Statements
06.Contiguous Arrays & Multi-Dimensional Matrices
07.Null-Terminated Strings & <string.h>
08.User Input, scanf & fgets Buffer Safety
09.Pointers, Memory Addresses & Dereferencing
10.Functions, Prototypes & Pass-by-Pointer
11.11. Recursion & High-Speed Function Pointers
12.File Streams: Create, Write & Read (fopen/fclose)
13.Structures, Unions & typedef Aliasing
14.Memory Word Alignment & Struct Padding
15.Enums & Bitwise Manipulation (Flags & Masks)
16.Dynamic Memory: malloc, calloc, realloc & free
17.Defensive Error Handling, errno & Assertions
18.Preprocessor Macros & Storage Classes
19.Fixed-Width Types (stdint.h), Math & Time (<time.h>)
20.Capstone: High-Performance In-Memory Key-Value Engine
Course Certificate0%

Complete all lessons to earn

LearnNull-Terminated Strings & <string.h>
7 / 20 0%
Lesson 07 of 20
6 mins read

Null-Terminated Strings & <string.h>

PreviousNext Lesson
In Plain English

A string in C is a sequence of characters terminated by a null byte \0 (ASCII 0). Use <string.h> functions like strlen(), strcmp(), strncpy(), and strncat() to inspect and manipulate text safely without overflowing fixed buffers.

The Mental Model

"A C string is a freight train where the last car is always a special red caboose (\0). Functions inspect cars one by one until they see the caboose."

Deep Dive: How It Works

The Null Byte (\0): Signals the end of character data; without it, functions read into garbage RAM.

Bounded Copies: strncpy() and strncat() prevent writing past destination buffer capacity.

Lexicographical Comparison: strcmp(a, b) returns 0 if strings match identically.

Syntax Blueprint

char buffer[32] = "ZenCompiler";
size_t len = strlen(buffer);

Buffer holds 12 characters plus \0, occupying 13 bytes total out of 32 allocated.

PointCore Rules to Remember

Point star
Null Termination: Always allocate 1 extra byte for the terminating \0 character in char arrays.
Point star
strcmp Behavior: Returns 0 for exact match, negative if a < b, and positive if a > b.
Point star
Safe Operations: Prefer bounded functions strncpy and strncat over unbounded strcpy and strcat.

Common Beginner Traps & How to Fix Them

Mistake:Using == to compare two C strings: if (str1 == str2).

Why it happens: == compares the pointer memory addresses, not the text contents.

How to fix: Always use strcmp(str1, str2) == 0.

Pro Tip: Always manually ensure null-termination after calling strncpy: dst[sizeof(dst) - 1] = '\0';

Live Interactive Example

Hit Run Code to see it live
Safe String Concatenation
C11 / GCC 9.2
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: strncat appends domain to header safely within remaining buffer capacity.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Verify Domain with strcmp

  • PointUse strcmp to check if domain equals "zencompiler.com".
  • PointIf it returns 0, print "Domain Match: YES\n".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates