Strings
Working with strings in C. String manipulation functions from the `string.h` library.
Strings in C
Declaring and Initializing Strings
In C, a string is essentially an array of characters terminated by a null character (\0
). Because C doesn't have a built-in string data type like some other languages, strings are represented and manipulated using character arrays and pointers.
Declaring and initializing strings in C can be done in several ways. The most common methods are using string literals and character arrays.
String Literals
A string literal is a sequence of characters enclosed in double quotes ("
). The C compiler automatically adds a null terminator (\0
) at the end of the string literal.
Example:
#include <stdio.h>
int main() {
char *message = "Hello, world!";
printf("%s\n", message);
return 0;
}
In this example, "Hello, world!"
is a string literal. The message
variable is a pointer to the first character of this string literal in memory. Note that string literals are generally stored in read-only memory, so you should not attempt to modify them directly.
Important considerations when using string literals:
- The memory allocated for a string literal is typically read-only. Attempting to modify it can lead to undefined behavior or a segmentation fault.
- String literals are implicitly null-terminated.
- You can use string literals to initialize character arrays or assign them to character pointers.
Character Arrays
A character array is an array of char
elements. You can initialize a character array with a string, and this is a modifiable string (unlike a string literal). When initializing with a string, space must be reserved for the null terminator as well.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello"; // Compiler automatically determines size
char str2[10] = "World"; // Explicitly sized array, plenty of space
char str3[6] = "C"; //Another sized array and assigned C
char str4[6];
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
printf("str3: %s\n", str3);
// Copying a string into str4
strcpy(str4, "World");
printf("str4: %s\n", str4);
str2[0] = 'w'; // Modifying the string in str2
printf("Modified str2: %s\n", str2);
return 0;
}
In this example:
str1
is initialized with the string "Hello". The compiler automatically allocates enough space to hold the string, including the null terminator. It’s equivalent tochar str1[6] = "Hello";
str2
is explicitly declared with a size of 10, which is sufficient to store "World" and the null terminator.str3
is explicitly declared with a size of 6, and contains the value "C".str4
is an uninitialized array of size 6.strcpy
is then used to copy the string literal "World" into the array. Make sure the array is large enough to hold the string being copied, including the null terminator.- Notice how in the code, the first index in str2 is modified
Key differences between string literals and character arrays:
- Character arrays are stored in modifiable memory. You can change the contents of a character array after it's been initialized.
- String literals are typically stored in read-only memory. Attempting to modify them is not allowed.
- Character arrays require you to specify or infer the size. String literals have their size automatically determined by the compiler.
Important Note: When using character arrays, be very careful about buffer overflows. Ensure that the array is large enough to hold the string you intend to store in it, including the null terminator. Functions like strcpy
are prone to buffer overflows if the destination buffer is too small. Safer alternatives, such as strncpy
, are available, but they require careful use to prevent truncation issues. For more robust string handling in modern C, consider using dynamic memory allocation with functions like malloc
and realloc
to resize string buffers as needed.