Strings#
The C Programming Language does not have a separate data type for strings. Instead, a string can be represented either as an array of characters or as a pointer to a character.
By convention, every string must end with a null terminator. Forgetting this is a common source of bugs because all standard library functions dealing with strings rely on it to know when a string ends.
String literals are given between double quotation marks: "string literal goes here". To represent special characters, we use their escape sequences. A null terminator is automatically inserted at the end of each literal by the compiler.
A string literals can be directly assigned to an array of characters or to a pointer to a character:
char helloAsArray[6] = "Hello"; // 6 instead of 5 because of the implicit null-terminator
char *helloAsPointer = "Hello";
When assigning to an array of characters, the array can be any number so long as it can fit the string literal:
char helloAsArray[27] = "Hello"; // just as valid
The literals themselves are stored in the resulting binary file.
Operations#
The header <string.h> provides functionality for various operations with strings.