In C, a string is not a built-in type. It is an array of char values that ends with a special null character, written '\0'. That terminator marks where the text stops, and the standard string functions depend on it to know where to stop reading. You will still meet these C-style strings in older code and in C libraries, even though modern C++ code usually uses std::string.
How a C string is stored
When you write char greeting[] = "Hello";, the compiler makes an array of six char values: the five letters and the terminator.
index: 0 1 2 3 4 5
char: H e l l o \0
The terminator takes up a slot, so the array must have room for it.
A first example
#include <stdio.h>
int main(void) {
char greeting[] = "Hello";
printf("%s\n", greeting);
printf("First letter: %c\n", greeting[0]);
return 0;
}
Output:
Hello
First letter: H
The %s format prints characters until it reaches the terminator.
Length: strlen and sizeof
strlen comes from <string.h>. It counts the characters before the terminator and does not count the terminator itself. sizeof reports the size of the array, which includes the terminator.
#include <stdio.h>
#include <string.h>
int main(void) {
char greeting[] = "Hello";
printf("strlen: %zu\n", strlen(greeting));
printf("sizeof: %zu\n", sizeof(greeting));
return 0;
}
Output:
strlen: 5
sizeof: 6
sizeof gives the size of the whole array only where the array is declared. If you pass the string to a function as a pointer, sizeof returns the size of the pointer instead. For that reason, use strlen or pass the length separately.
Making room for the text
The declaration char name[10] = "Ann"; creates an array of 10 characters. The text uses four of them (three letters and the terminator), and the remaining slots are set to zero.
The array must always be large enough for the text plus the terminator. If you write past the end of an array, the result is undefined behavior, which can crash a program or corrupt other data.
String literals
Text in double quotes, such as "Hello", is a string literal. You can point a pointer at one:
const char *message = "Hello";
Treat literal text as read-only, because changing its characters is undefined behavior. Declaring the pointer as const char * makes the compiler report accidental changes.
Walking through the characters
Because each string ends with '\0', a loop can stop when it reaches that character:
#include <stdio.h>
int main(void) {
char word[] = "Cat";
for (int i = 0; word[i] != '\0'; i++) {
printf("%c\n", word[i]);
}
return 0;
}
Output:
C
a
t
For more on this pattern, see how to iterate through a string in c.
What goes wrong
Most string bugs in C come from the terminator or from the array size:
- A missing terminator. The declaration
char bad[3] = {'C', 'a', 't'};has no'\0'. Functions such asprintf("%s", bad)andstrlen()keep reading past the end of the array, which is undefined behavior. - An array that is too small. Copying a longer string into it writes past the end.
- Joining strings. The destination needs room for both parts plus the terminator. The careful way to join them is covered in how to concatenate strings in c.
Comparing C strings
You cannot check whether two C strings hold the same text with ==, because == compares the addresses of the arrays. The standard function for comparing text is strcmp, and its return values are explained in what does strcmp do in C?
C++ note
If you are learning C++, std::string keeps track of its own length and memory, so you rarely have to manage a terminator yourself. You still need <cstring> for functions such as std::strlen and std::strcmp when you work with C-style strings.
In short, a C string is a char array that ends with '\0'. Keep the array large enough for the text and the terminator, and use the library functions that understand that terminator.