In C, a string is defined as a sequence of characters, stored in an array of type char. A string in C is terminated by a special null character (‘\0’), which marks the end of the string. This is a key difference from arrays that don’t necessarily have a terminator.
Syntax:
char str[] = "Hello, Friends!";
Note:
- It creates an array of characters to hold the string and string should be in double quote.
- It automatically adds the null terminator (‘\0’) to the end of the string.
In the above code, str will contain
{'H', 'e', 'l', 'l', 'o', ',', ' ', 'F', 'r', 'i', 'e', 'n', 'd', 's', '!', '\0'}
How to Access a String
In C, strings are arrays of characters. You can access individual characters in the string using array indexing or pointer notation. Both are equivalent because strings in C are pointers to the first character of the array.
Example 1: Accessing Characters Using Indexing
#include <stdio.h>
int main() {
char str[] = "My World";
// Access characters by index
printf("First character: %c\n", str[0]); // 'm'
printf("Second character: %c\n", str[1]); // 'y'
printf("Third character: %c\n", str[2]); // ''
printf("Fourth character: %c\n", str[3]); // 'W'
return 0;
}
Output:
Second character: y
Third character:
Fourth character: W
Note: to access a string, start from 0 index.
Example 2: Accessing Characters Using Pointers
#include <stdio.h>
int main() {
char str[] = "My World";
char *ptr = str; // Pointer to the first character of the string
// Access characters using pointer notation
printf("First character: %c\n", *ptr); // 'M'
printf("Second character: %c\n", *(ptr + 1)); // 'y'
printf("Third character: %c\n", *(ptr + 2)); // ''
printf("Fourth character: %c\n", *(ptr + 3)); // 'W'
return 0;
}
Output:
Second character: y
Third character:
Fourth character: W
How to Modifying a String
In C, you can modify individual characters in a string (which is an array of char) by using array indexing or pointer notation. However, you cannot modify a string literal (such as “Hello”) because string literals are stored in read-only memory.
Example 1: Modifying Characters Using Indexing
#include <stdio.h>
int main() {
char str[] = "My World";
// Modify characters in the string
str[0] = 'm'; // Change the first character to 'm'
str[3] = 'V'; // Change the third character to 'V'
printf("Modified string: %s\n", str); // Output: "my Vorld"
return 0;
}
Output:
Example 2: Modifying Characters Using Pointers
#include <stdio.h>
int main() {
char str[] = "My World";
char *ptr = str; // Pointer to the first character of the string
// Modify characters using pointer notation
*(ptr) = 'm'; // Change the first character to 'm'
*(ptr + 3) = 'V'; // Change the third character to 'V'
printf("Modified string: %s\n", str); // Output: "my Vorld"
return 0;
}
Output:
Using Strings in Loops
Strings in C are essentially arrays, so you can use loops to process each character one by one. You can either loop until the null terminator (‘\0’) is reached, or use a fixed number of iterations if you know the string length
Example: Using a for Loop to Iterate Through a String
#include <stdio.h>
int main() {
char str[] = "My World";
// Loop through the string and print each character
for (int i = 0; str[i] != '\0'; i++) {
printf("Character at index %d: %c\n", i, str[i]);
}
return 0;
}
Output:
Character at index 1: y
Character at index 2:
Character at index 3: W
Character at index 4: o
Character at index 5: r
Character at index 6: l
Character at index 7: d
C String – Interview Questions
Q 1: What is a string in C?
Q 2: Which header file is required for string functions?
Q 3: How are strings stored in memory in C?
Q 4: What is the difference between char str[] and char *str?
Q 5: Why is null character important in C strings?
C String – Objective Questions (MCQs)
Q1. What is a string in C?
Q2. Which of the following correctly declares a string in C?
Q3. Which header file is required to use standard string functions like strlen() and strcpy()?
Q4. What does the strlen() function return?
Q5. How do you compare two strings str1 and str2 in C?