A string consists of an array of characters and is delimited by double quotes. There is no string type for storing strings in C. Instead, strings are commonly assigned to a character array, as shown here.
Strings in C are terminated with a null character
\0, which is used to know where the string ends. The
null character is added automatically by the compiler for quoted strings, as in the previous example. The same statement can also be written using regular array initialization syntax, in which case the null character needs to be explicitly included.
char myString[3] = { 'H', 'i', '\0' };
Both statements produce the same result: a char array with three elements. Note that individual characters are delimited by single quotes and not double quotes. To print a string the format specifier
%s is used with the
printf function, which outputs the literals of the string until the null character is encountered.
printf("%s", myString); /* "Hi" */
As an alternative to the
character array, a char pointer may be set to point to a string. The string is then automatically stored in the compiled file, giving the pointer a location to point to. In most compilers this location is a read-only block, so unlike with the char array, the characters in this string cannot be changed.
char* ptr = "Hi";
printf("%s", ptr); /* "Hi" */
Escape Characters
To add new lines to a string, the
escape character \n is used to represent a line break.
printf("First line\nSecond line");
This backslash notation is used to write special characters that are difficult or impossible to type on a regular keyboard. In addition to the newline and null characters, there are several other such characters, as seen in the following table.
Character | Meaning | Character | Meaning |
|---|
\n
| Newline |
\f
| Form feed |
\t
| Horizontal tab |
\a
| Alert sound |
\v
| Vertical tab |
\'
| Single quote |
\b
| Backspace |
\"
| Double quote |
\r
| Carriage return |
\\
| Backslash |
\0
| Null character |
\?
| Question mark |
\000
| Octal number (1-3 digits) |
\xhh
| Hexadecimal number |
Any one of the 128 ASCII characters can be expressed by
writing a backslash followed by the ASCII code for that
character, represented as either an octal or hexadecimal number. This is illustrated here, where the newline character is represented in three different ways:
char line = '\n'; /* escape code */
line = '\012'; /* octal notation */
line = '\x0A'; /* hexadecimal notation */
String Functions
Because strings in C are arrays, the only way to make changes to them is to change each element in the array. To simplify common string operations, the standard header
string.h includes a collection of functions for manipulating null-terminated strings. Consider the following code, which will be used as the template for the following string examples.
#include <stdio.h>
#include <string.h>
int main(void) {
char s1[11] = "Hello";
char s2[11] = "World";
int result;
}
The string function
strcat (string concatenation)
appends the second string onto the first string. For this to work, it is important that the destination is large enough to hold the entire string.
/* Append s2 to s1 */
strcat(s1, s2); /* s1 = "HelloWorld" */
Another string function is
strcpy
, which copies the characters in the second string into the first string. The function stops when the terminating null character for the second argument is reached.
/* Copy s1 into s2 */
strcpy(s2, s1); /* s2 = "HelloWorld" */
A
string can be compared with another string using the
strcmp function. If all characters match, the function returns zero.
/* Compare s1 and s2 */
result = strcmp(s1, s2); /* 0 (equal) */
When manipulating strings, it is important to take their lengths into account to avoid overwriting adjacent memory. The length of a string stored in a char array can be found with the
strlen function
. With regular (statically allocated) strings, the allocated size can also be retrieved using the
sizeof operator.
/* Length of s1 (excluding null char) */
result = strlen(s1); /* 10 */
/* Allocated size for s1 */
result = sizeof(s1); /* 11 */
The next example
shows how the
sizeof operator
can be used to allocate a character array large enough to hold two other strings. Note that the resulting string will only have one null character, so the size of its allocated array can be one character less than the sizes of the two other strings.
char a[] = "Hello"; /* sizeof(a) = 6 */
char b[] = "World"; /* sizeof(b) = 6 */
char c[sizeof(a) + sizeof(b) -1]; /* sizeof(c) = 11 */
strcpy( c, a );
strcat( c, b ); /* "HelloWorld" */