C Strings
What is a String in C?
In C, strings are arrays of characters laid out in memory, terminated by a special null character ('\0') to indicate where the string ends.
Example:
char greeting[] = "Hello";
In the example above:
- The string "Hello" in C is internally stored as a character array: {'H', 'e', 'l', 'l', 'o', '\0'}, with the null terminator marking the end.
The last character \0 is automatically added when using a string literal in double quotes.
Creating Strings
In C, there are two main methods to define and initialize strings.
1. Using Array Notation
char text1[] = "World";
2. Manually Specifying Characters
char text2[] = {'W', 'o', 'r', 'l', 'd', '\0'};Both methods create the same string, but the second one gives you full control over each character.
Accessing String Characters
You can pick out individual characters using array indexes, just like with regular arrays:
printf("%c", greeting[0]); // Output: HStrings and printf()
In printf(), the %s specifier outputs the complete content of a string.
char message[] = "Good Day!";
Printf("%s", message); // Output: Good Day! Looping Through a String
A for loop can be used to iterate over each character in a string until the null terminator is encountered.
char name[] = "Anna";
int i;
for (i = 0; name[i] != '\0'; i++) {
printf("%c\n", name[i]);
} This prints one character per line.
String Functions in C
The C standard library includes a collection of functions designed to handle string operations with ease and efficiency.
1. strlen() – String Length
Calculates the length of the string, excluding the trailing null character.
#include <string.h>
Printf("%zu", strlen("Coffee")); // Output: 6 2. strcpy() – Copy Strings
Copies contents from one string to another.
char source[] = "Data"; char target[20]; Strcpy(target, source);
3. strcat() – Combine Strings
Joins one string to the tail of another, effectively combining them into a single sequence.
char first[20] = "Hello "; char second[] = "World!"; Strcat(first, second);
4. strcmp() – Compare Strings
Compares two strings and returns:
- 0 if both are identical
- Returns a non-zero value—positive or negative—when the strings are not identical.
int result = strcmp("abc", "abc"); // result = 0Note on String Memory
When using strings:
- Ensure the array has sufficient space for all characters along with the terminating null byte (\0).
- Always include
for string functions. - Be careful when using pointers to string literals, as modifying them is undefined behavior.
Example Program
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello";
char str2[] = " World";
strcat(str1, str2);
printf("Concatenated: %s\n", str1);
printf("Length: %zu\n", strlen(str1));
return 0;
} OutPut:
Concatenated: Hello World Length: 11
Summary
- Use %s to print them.
- Functions such as strlen, strcpy, strcat, and strcmp provide convenient tools for managing and manipulating strings in C.
- Always be cautious with buffer size and memory boundaries.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C Tutorial visually:
What You'll Learn:
- 📌 #21 C Strings | [2025] C Programming For Beginners
- 📌 C_63 Strings in C-part 2 | Read a String using scanf and gets function