Nearby lessons
63 of 124C - String Introduction
C has no built-in string type — a string in C is an array of characters ending with the null character '\0'. Learn what that means, why the terminator matters, and how strings differ from character arrays.
There Is No string Type
Languages like Java and Python give you a String type. C does not. In C a string is simply an array of char with one rule attached: the last useful character must be followed by the null character '\0'.
The Null Terminator
'\0' is the character whose numeric value is zero. It is the sentinel that tells every string function "stop here".
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Character | H | e | l | l | o | \0 |
| ASCII value | 72 | 101 | 108 | 108 | 111 | 0 |
Three Different Zeros
These look similar and are constantly confused. They are not the same:
Why the Extra Byte Matters
An array of exactly 5 chars cannot hold the 5-letter string "Hello" — there is no room for the terminator:
strlen vs sizeof
This pair is the most common string interview question, and the distinction is simple:
strlen(s) | sizeof(s) | |
|---|---|---|
| Measures | Characters before \0 | Bytes reserved for the array |
Counts \0? | No | Yes |
| When decided | At runtime, by scanning | At compile time |
For char s[20]="Hi" | 2 | 20 |
A Character Array Without \0
Omit the terminator and you no longer have a string — you have a character array. printf("%s") will run past the end and print whatever follows:
Strings Are Not Assignable
Because a string is an array, the array rules apply: you cannot assign or compare with = and ==:
Common Mistakes
- Sizing the array exactly —
char s[5] = "Hello";leaves no room for\0. - Confusing
'A'and"A"— the first is 1 byte, the second is 2 bytes (the letter plus\0). - Comparing with
==— that compares pointers. Usestrcmp. - Assuming
strlencounts the terminator — it does not. - Overwriting the
\0— one stray write and the string loses its end marker.
strcpy simply copy until they see \0 — even if that means running far past the end of your array. Always reserve space for the terminator.- C has no string data type; a string is char[] ending in \0.
- The null character \0 marks where the string ends.
- A string of n characters needs n + 1 bytes.
- \0 is the character with value 0 — not the digit 0 and not a space.
- Without \0, printf keeps reading past the end into garbage.