What is String in C?
- Siddharth Sharma
- Nov 6, 2024
- 3 min read
a string is a sequence of characters stored in a contiguous block of memory. Strings in C are essentially arrays of characters, ending with a special character called the null character ('\0'). This null character tells the compiler where the string ends in memory. Since C doesn’t have a dedicated string data type like some other languages (e.g., Python or Java), strings are usually represented as char arrays or char pointers.
Key Characteristics of Strings in C
Null-Terminated: Every string in C ends with the null character '\0'. This marks the end of the string, allowing functions that handle strings to know where the string stops.
Fixed Length: When you declare a string as a character array, you need to specify its length at the time of declaration.
Mutable: Characters within a string can be modified if the string is defined in a mutable memory location.
Declaring and Initializing Strings in C
There are two common ways to declare and initialize strings in C:
Using a Character Array:
char *str = "Hello, World!";This declares a character array of size 20, large enough to hold "Hello, World!" plus the null character at the end.
The string is automatically null-terminated by the compiler.
Using a Character Pointer:
char *str = "Hello, World!";This creates a pointer str that points to the string "Hello, World!" stored in a read-only section of memory.
Be cautious: strings declared as pointers cannot be modified directly since they might point to read-only memory.
Example of Using Strings in C
Here’s a simple example to show how strings work:
#include <stdio.h>
int main() {
char greeting[] = "Hello, World!";
printf("%s\n", greeting); // %s format specifier is used to print strings
return 0;
}Explanation:
greeting is a character array initialized with the string "Hello, World!".
%s is the format specifier for strings in printf().
When printf() encounters greeting, it prints all characters in greeting until it reaches the null character '\0'.
Common String Operations in C
The C standard library provides several functions for handling strings, located in the string.h header. Here are some commonly used functions:
strlen(): Calculates the length of a string (excluding the null character).
int len = strlen(greeting); // Returns 13 for "Hello, World!"strcpy(): Copies one string to another.
char destination[20];
strcpy(destination, greeting); // Copies "Hello, World!" to destinationstrcat(): Concatenates (appends) one string to the end of another.
char str1[20] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2); // str1 becomes "Hello, World!"strcmp(): Compares two strings lexicographically.
int result = strcmp("abc", "abc"); // Returns 0 if equal, non-zero otherwisestrchr() and strstr(): Find the first occurrence of a character or substring in a string.
char *ch = strchr(greeting, 'W'); // Finds 'W' in "Hello, World!"
char *substr = strstr(greeting, "World"); // Finds "World" in greetingExample Program Using String Functions
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = "World";
// Concatenate str2 onto str1
strcat(str1, str2);
printf("Concatenated string: %s\n", str1); // Outputs "HelloWorld"
// Find length of str1
int len = strlen(str1);
printf("Length of string: %d\n", len); // Outputs length of "HelloWorld"
// Copy str2 to a new string
char str3[20];
strcpy(str3, str2);
printf("Copied string: %s\n", str3); // Outputs "World"
// Compare two strings
int result = strcmp(str1, "HelloWorld");
if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}Summary
Strings in C are arrays of characters terminated by a null character ('\0').
They can be declared as character arrays or character pointers.
String functions from string.h provide useful operations like concatenation, copying, comparison, and searching.
Important Note: Always ensure enough space in character arrays to store both the string content and the null character.




Comments