C Strings
Text is everywhere in programs: names, messages, file paths. Unlike most modern languages, C has no dedicated string type. A string in C is simply an array of char that ends with a special character, the null terminator '\0' (a byte with the value 0). Every function that works with strings relies on that terminator to know where the text stops.
Creating a string
The easiest way is to initialise a char array with a string literal in double quotes. The compiler copies the characters and adds the '\0' for you:
#include <stdio.h>
int main(void) {
char greeting[] = "Hello";
printf("%s\n", greeting);
printf("sizeof(greeting) = %zu\n", sizeof(greeting));
return 0;
}Hello
sizeof(greeting) = 6
"Hello" has five letters, but the array is six bytes long because of the hidden terminator:
| Index | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| Character | 'H' |
'e' |
'l' |
'l' |
'o' |
'\0' |
The %s specifier prints characters starting at the given address until it reaches '\0'.
Accessing and changing characters
Since a string is an array, you can read and modify single characters with an index. Use %c to print one character:
#include <stdio.h>
int main(void) {
char word[] = "cat";
printf("First letter: %c\n", word[0]);
word[0] = 'b';
printf("Changed word: %s\n", word);
word[2] = 'g';
printf("Changed again: %s\n", word);
return 0;
}First letter: c
Changed word: bat
Changed again: bag
Looping through a string
Because the end is marked with '\0', you can loop until you hit it — no length needed:
#include <stdio.h>
int main(void) {
char text[] = "Programming in C";
int vowels = 0;
int length = 0;
for (int i = 0; text[i] != '\0'; i++) {
char ch = text[i];
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
}
length++;
}
printf("\"%s\" has %d characters and %d vowels\n", text, length, vowels);
return 0;
}"Programming in C" has 16 characters and 4 vowels
(The \" inside the format string prints a double quote; escape sequences are covered in the next lesson.)
Specifying the size
You can give the array a size explicitly. Make it at least one larger than the text so there is room for '\0'. Unused space is filled with zeros:
#include <stdio.h>
int main(void) {
char city[20] = "Oslo";
printf("%s uses %zu bytes of storage\n", city, sizeof(city));
// Building a string character by character
char code[4];
code[0] = 'A';
code[1] = 'B';
code[2] = 'C';
code[3] = '\0'; // without this, printing would run past the array!
printf("code = %s\n", code);
return 0;
}Oslo uses 20 bytes of storage
code = ABC
Watch out:
char name[5] = "Hello";compiles in C but leaves no room for the terminator, sonameis not a valid string. Printing it reads past the end of the array.
Array strings vs. pointer strings
You will often see strings written as const char *:
#include <stdio.h>
int main(void) {
char editable[] = "apple"; // an array holding a copy of the text
const char *fixed = "banana"; // a pointer to a read-only literal
editable[0] = 'A'; // fine
printf("%s %s\n", editable, fixed);
// fixed[0] = 'B'; // not allowed: string literals are read-only
return 0;
}Apple banana
char s[] = "text"; |
const char *s = "text"; |
|
|---|---|---|
| Storage | the array owns a copy | points to the literal stored by the compiler |
| Can modify characters | yes | no (undefined behaviour) |
sizeof(s) |
length + 1 | size of a pointer (8) |
| Can point elsewhere later | no | yes |
What you cannot do
Strings are arrays, so the array rules apply:
char a[10] = "hi";
a = "bye"; // error: arrays cannot be assigned
if (a == "hi") {} // compares addresses, not text — always use strcmpTo copy, join, compare or measure strings you use functions from <string.h> such as strcpy, strcat, strcmp and strlen. They are covered in C String Functions.
Tip: A single character uses single quotes (
'A', typechar); a string uses double quotes ("A", two bytes:'A'and'\0').
Test yourself
How many bytes does char s[] = "code"; occupy?
- 4
- 5
- 8
- It depends on the compiler
Which format specifier prints a whole string?
- %c
- %d
- %s
- %str
Why does if (name == "Bob") not compare the text?
- Strings cannot be in if statements
- It compares memory addresses, not characters
- It needs single quotes
- It does compare the text correctly