C
Character Strings in C
🏠 Home Lecture 15: Character Strings
Text Data in C

Character Strings

Unlike Python or Java, C does not have a built-in String data type. Instead, a string is simply a 1-D array of characters, terminated by a special invisible null character \0.

How many bytes of memory are needed to store the string "HELLO"?
intro.cString Array
#include <stdio.h> int main(void) { // A string is just a character array char greeting[] = "Hello, World!"; printf("%s\n", greeting); return 0; }
#

Array of Characters

Under the hood, strings use the char data type (1 byte per character).

\0

Null Terminator

Every string must end with the null character \0 (ASCII value 0) so C knows where it stops.

" "

Double Quotes

Strings are enclosed in " ", while single characters are enclosed in ' '.

%s

Format Specifier

Use %s in printf and scanf to read or write an entire string.

Syntax

Declaration & Initialization

You can initialize strings character-by-character (tedious) or using a string literal shortcut. Both achieve the same memory layout.

Method 1: Character Array InitTedious
// Must manually add '\0' at the end! char word1[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Omitting array size is fine, C counts them. char word2[] = {'H', 'i', '\0'};
Method 2: String Literal InitRecommended
// C automatically appends the '\0' for you! char word1[6] = "Hello"; // Auto-sized to 6 bytes (5 for "Hello" + 1 for '\0') char word2[] = "Hello"; // ⚠️ BUG: Size 5 is too small for "Hello" + '\0' char word3[5] = "Hello"; // Missing terminator!
Visualizing RAM

Memory Layout & The Null Terminator \0

Arrays in C don't store their own length. The only way C functions like printf know when to stop printing a string is by looking for the Null Terminator (\0).

Interactive Memory Visualizer

Type a word below to see how it's stored in a 10-byte character array:

Character Array in RAM: char str[10] = "HELLO";
Total array capacity: 10 bytes. Characters used: 5. Null Terminator at index: 5.
Input / Output

Reading & Writing Strings

To read a string, use scanf or fgets. Be careful: scanf("%s") stops reading at the first space!

scanf("%s", str);

Reads characters until it encounters a whitespace (space, tab, or newline). Note: No `&` before `str` because an array name is already an address!

fgets(str, size, stdin);

Safely reads a full line of text, including spaces, up to `size - 1` characters. Recommended for reading sentences.

Simulation: scanf vs fgets

Console Output:
Ready...
Standard Library

Common <string.h> Functions

C provides the <string.h> library containing powerful utility functions to manipulate strings without manually writing loops.

strlen(str)

Returns the length of the string (excluding the null terminator).

strcpy(dest, src)

Copies the contents of string `src` into `dest`. Overwrites `dest`.

strcmp(str1, str2)

Compares two strings. Returns 0 if they are exactly identical.

string_funcs.cUsing <string.h>
#include <stdio.h> #include <string.h> // Required for string functions int main(void) { char s1[20] = "Apple"; char s2[20]; // 1. Get length int len = strlen(s1); // len is 5 // 2. Copy string strcpy(s2, s1); // s2 now contains "Apple" // 3. Compare if (strcmp(s1, s2) == 0) { printf("Strings match!\n"); } return 0; }
Question 1 of 4
Loading quiz...