Strings in memory Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

A string is a char array with an end mark

C stores text as an array of char. After the last character comes the null character '\0' (the byte 0), which marks where the text ends — every string function walks the array until it meets it. So the array must always have one cell more than the number of characters. A string literal like "Hi" already contains that '\0': it is 3 bytes.

char a[6] = "Hello";        // H e l l o \0
char b[]  = "Hi";           // size 3: H i \0
char c[10] = "Hi";          // H i \0 \0 … (rest filled with \0)
char d[5] = {'H','e','l','l','o'};   // NOT a string: no \0!

printf("%s\n", a);          // prints until \0
printf("%c\n", a[1]);       // e — one character
Ways to work with strings
Code What it does Watch out
char s[20] = "text"; Array with the text + '\0' Size ≥ length + 1
printf("%s", s); Prints up to '\0' Pass s, not &s or *s
scanf("%19s", s); Reads one word (stops at a space) No &; give a width limit
fgets(s, sizeof s, stdin); Reads a whole line, keeps '\n' Remove the '\n' if not wanted
s[i] One character (a char) i must be < the length
for (i = 0; s[i] != '\0'; i++) Visits every character Stops before the terminator

Strings in memory

  1. Each character is one char (1 byte) holding its ASCII code.
  2. '\0' is the char with value 0 — not the digit '0' (48).
  3. "Hi" is a string literal: H, i and '\0' — 3 bytes.
  4. An initialised array gets '\0' automatically if there is room.
  5. Length = characters before '\0'; sizeof = the whole array.

Printing and walking

  1. %s needs the address of the first char: printf("%s", s).
  2. printf prints character after character until '\0'.
  3. Without '\0' it runs past the array and prints garbage (or crashes).
  4. Loop with s[i] != '\0' — you do not need to know the length.
  5. Change one character with s[i] = 'X'; the array must not be a literal pointer.

Reading text safely

  1. scanf("%s", s) — no & because s is already an address.
  2. %s stops at the first space, tab or newline: it reads one word.
  3. Always limit it: %19s for char s[20], or the input can overflow the array.
  4. fgets(s, sizeof s, stdin) reads a line with spaces, and never overflows.
  5. fgets keeps the '\n': remove it with s[strcspn(s, "\n")] = '\0'. Never use gets().
Walkthrough

Read a full name and count its vowels

Every line, explained.

1 #include <stdio.h>
2 #include <string.h> For strcspn.
3
4 int main(void)
5 {
6 char line[50]; Room for 49 characters + '\0'.
7 printf("Your name: ");
8 fgets(line, sizeof line, stdin); The user types Ada Lovelace — the space is kept.
9 line[strcspn(line, "\n")] = '\0'; Replace the '\n' fgets kept with '\0'.
10 int vowels = 0;
11 for (int i = 0; line[i] != '\0'; i++) { Walk until the terminator.
12 char c = line[i];
13 if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
14 vowels++; a, o, e, a, e
15 }
16 printf("Hello, %s! (%d vowels)\n", line, vowels); Prints Hello, Ada Lovelace! (5 vowels)
17 return 0;
18 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

No room for '\0'

✗ Wrong
char s[5] = "Hello";   // 5 letters, no terminator
✓ Right
char s[6] = "Hello";   // or: char s[] = "Hello";

The array is full with the letters, so '\0' is dropped and %s runs past the end.

& with %s in scanf

✗ Wrong
scanf("%s", &name);
✓ Right
scanf("%19s", name);

An array name already is an address. Also give a width so long input cannot overflow.

Expecting %s to read a full name

✗ Wrong
scanf("%s", name);   // types: Ada Lovelace
// name = "Ada"
✓ Right
fgets(name, sizeof name, stdin);

%s stops at the first space; the rest stays in the input buffer for the next read.

Comparing strings with ==

✗ Wrong
if (answer == "yes")
✓ Right
if (strcmp(answer, "yes") == 0)

== compares two addresses, never the text. strcmp compares character by character (Module 28).

Assigning a string with =

✗ Wrong
char s[10];
s = "hi";   // error
✓ Right
char s[10];
strcpy(s, "hi");

Arrays cannot be assigned. Initialise when declaring, or copy with strcpy.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

How many bytes does the string literal "cat" take?

Question 2

Which character ends every C string?

Question 3

char s[] = "Hello"; s[2] = '\0'; printf("%s", s); prints?

Question 4

The user types "New York". What does scanf("%s", city) store?

Question 5

char s[] = "Hi"; — what is sizeof s?

Assessment

Strings quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.