Strings serve the purpose of storing text or sequences of characters.
For instance, “Hello World” constitutes a string of characters.
Unlike numerous other programming languages, C lacks a built-in String type for straightforward creation of string variables. Instead, you must utilize the char type and establish an array of characters to represent a string in C:
char greetings[] = “Hello World!”; |
It’s important to use double quotes ( “” ) when defining strings.
To output the string, you can employ the printf() function along with the format specifier %s, indicating to C that we are dealing with strings:
char greetings[] = “Hello World!”; printf(“%s”, greetings); |
As strings are essentially arrays in C, you can access a string by referencing its index number within square brackets [ ].
The following example outputs the first character (index 0) in the variable greetings:
char greetings[] = “Hello World!”; printf(“%c”, greetings[0]); |
It’s worth noting that we must utilize the %c format specifier to print a single character. |
To modify the value of a specific character within a string, reference its index number and employ single quotes:
char greetings[] = “Hello World!”; greetings[0] = ‘J’; printf(“%s”, greetings); // Outputs Jello World! instead of Hello World! |
You can iterate through the characters of a string by utilizing a for loop:
char carName[] = “Volvo”; int i; for (i = 0; i < 5; ++i) { |
As mentioned in the arrays chapter, you can utilize the sizeof formula (rather than manually specifying the size of the array in the loop condition, such as i < 5) to enhance the sustainability of the loop:
char carName[] = “Volvo”; int length = sizeof(carName) / sizeof(carName[0]); int i; for (i = 0; i < length; ++i) { |
In the preceding examples, we employed a “string literal” to generate a string variable, which is the simplest approach in C.
It’s important to recognize that you can also create a string with a predefined set of characters. The following example will yield the same outcome as the initial example on this page:
char greetings[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’, ‘\0’}; printf(“%s”, greetings); |
Why do we include the \0 character at the end? This character, known as the “null terminating character,” must be included when constructing strings using this method. It informs C that the string has reached its conclusion. |
The distinction between the two methods of creating strings lies in the ease of writing. With the first method, you don’t need to include the \0
character manually, as C handles it for you.
It’s important to recognize that the size of both arrays remains the same: They each consist of 13 characters, including the \0
character (note that even spaces count as characters).
char greetings[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’, ‘\0’}; char greetings2[] = “Hello World!”; printf(“%lu\n”, sizeof(greetings)); // Outputs 13 |
Utilize strings to craft a basic welcome message:
char message[] = “Good to see you,”; char fname[] = “John”; printf(“%s %s!”, message, fname); |