← 🔤 Strings & Characters
Subtopic 1 of 3

4.1Strings, the null terminator and arrays vs literals

The idea

A C string is just a char array that ends with a zero byte ('\0'). The zero byte tells every library function where the text stops.

Think of it like this: A train of letter wagons with a final empty wagon marking the end of the train.

Why it exists

Off-by-one in storage (the extra byte), sizeof versus strlen, and array versus literal pointer are all standard traps.

How it works

  1. "abc" is stored as 'a','b','c','\0': 4 bytes.
  2. strlen counts characters before the terminator (3); sizeof counts the array (4).
  3. char s[] = "abc" makes a modifiable copy; char *t = "abc" points to a read-only literal.
  4. A pointer has size 8 regardless of the string length.

Worked example: sizeof and strlen

For char s[] = "abc"; char *t = "abc";: sizeof(s) = 4, strlen(s) = 3, sizeof(t) = 8, s[3] = 0.

Now play with it

  • →Run the lab on sizeof(s), sizeof(t), strlen(s) and s[3].
  • →Try s == t to see that two equal-looking strings are different addresses.

s is an ARRAY holding a copy of "abc" (4 bytes with the terminator). t is a POINTER to a read-only literal.

type of the expressionunsigned long
value4
Memory (one box per element, address below)
97
s[0]
@100
98
s[1]
@101
99
s[2]
@102
0
s[3]
@103
105
t
@104

Gold = the object the expression names. Blue = the element a pointer result points to. Each box stands for one element; real addresses grow by sizeof(element).

Check yourself

How many bytes does char name[] = "GATE"; occupy?

Remember: String = chars + '\0'; sizeof counts the terminator, strlen does not.
Exam notes (the dense version)

A C string is a char array ending in '\0'. char s[] = "abc" is a modifiable array of 4 chars. char *s = "abc" points to a read-only literal: writing through it is undefined. sizeof("abc") = 4 but strlen("abc") = 3. The terminator is not counted by strlen but must fit in the array.

Previous year questions: Strings & Characters (3)

PYQs are grouped by topic, so this is the full set for Strings & Characters; try the ones that match this subtopic. Open all 3 on the topic page.