Strings in C are not a separate type. A C string is an array of char whose last element is the null character '\0' (value 0), and every string function relies on that terminator to find the end. This guide on strings in C explains how they are laid out in memory, how to declare them and what goes wrong when the terminator is missing.
Because the length is not stored anywhere, a string of 5 visible characters needs 6 bytes. Forgetting that one extra byte is the source of many classic bugs.
When you write the literal "Hello", the compiler creates 6 bytes: 'H', 'e', 'l', 'l', 'o' and '\0'.
The compiler sized word as 6 bytes by counting the characters and the terminator. The final value 0 is the null character. It is different from the character '0', which has the value 48 in ASCII, and from the pointer NULL.
There are three common forms, and they behave differently.
| Declaration | Where the bytes live | Can you change them? |
|---|---|---|
char a[] = "Hello"; | Array on the stack (a copy) | Yes |
const char *c = "Hello"; | Read-only literal storage | No, undefined behaviour if you try |
char *p = malloc(6); | Heap | Yes, after you fill it |
Always use const char * for pointers to string literals. Writing c[0] = 'J'; through a pointer to a literal is undefined behaviour and usually crashes with a segmentation fault. An array initialised from a literal is a separate copy that you may change freely.
Because there is no stored length, the length is found by walking from the start until the terminator. That is what strlen does, and the count excludes the terminator.
Length is the number of characters before the null; size is the bytes the array occupies. Since strlen loops, it costs time proportional to the length, so avoid calling it on every iteration of a loop condition when the string does not change.
Print a string with %s, which prints characters until it meets the null. Read one with fgets, which stops at the buffer size and keeps you safe from overflow.
Never use gets; it was removed from the language in C11 because it cannot limit input. Using scanf("%s", name) without a width has the same overflow danger, so write scanf("%31s", name) if you must use it.
If the null is missing, string functions keep reading memory past the array until they happen to hit a zero byte. The result is garbage output, a crash or a security hole.
Here the array has exactly 5 bytes, all letters, so printf reads beyond it. Note that char ok[5] = "Hello"; is a related trap: in C it is legal, but the terminator is dropped, so the result is not a valid string. Give the array at least one extra byte, or let the compiler size it with [].
An array name cannot be assigned to, and == on two strings compares addresses, not text.
Use the functions from string.h for copying and comparing. They are the subject of the next topic.
No. strlen("Hello") returns 5, while the array holding it needs at least 6 bytes.
'\0' is the null character with value 0 and ends a string. '0' is the digit character with value 48. NULL is a null pointer constant, not a character.
Literals are stored in read-only memory. Copy the text into a char array if you need to change it.
A char string holds bytes, so UTF-8 text works as long as you remember that one character can use several bytes and strlen counts bytes, not characters.
A C string is a char array terminated by '\0', so it always needs one byte more than its visible length. Declare modifiable strings as arrays, point to literals with const char *, read with fgets, and always leave room for the terminator.