Index
String
A string is a sequence of characters stored in contiguous memory locations, terminated by a null character '\0'
. Strings in C are represented as arrays of characters (char[]
) where each element of the array holds a single character. The null character marks the end of the string.
Here are some key characteristics of strings in C:
- Null Termination: C strings are null-terminated, meaning that the null character
'\0'
is used to mark the end of the string. This character is automatically appended to the end of string literals. - String Literals: A string literal is a sequence of characters enclosed in double quotes (
"
). For example,"hello"
is a string literal representing the string “hello”. String literals are automatically null-terminated. - Character Array: Strings in C are typically represented as arrays of characters (
char[]
). Each element of the array holds a single character, and the last element is always the null character to signify the end of the string. - Character Pointer: Strings can also be represented using pointers to characters (
char*
). A pointer to the first character of the string is sufficient to access the entire string. - Standard Library Functions: C provides a set of standard library functions for working with strings, such as
strcpy
,strcat
,strlen
,strcmp
, etc. These functions are declared in thestring.h
header file.
Declaring and Initializing a String
You can declare and initialize a string in C in two main ways:
Method 1: Character Array
#include <stdio.h>
int main() {
char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Declare and initialize a string
printf("%s\n", str); // Print the string
return 0;
}
Output:
Hello
Explanation:
- The string
"Hello"
is stored in a character array, with the last character being the null terminator (\0
). - The
%s
format specifier inprintf
is used to print strings.
Method 2: String Literal
#include <stdio.h>
int main() {
char str[] = "Hello"; // Automatically adds the null terminator at the end
printf("%s\n", str); // Print the string
return 0;
}
Output:
Hello
- Explanation:
- Here, the string
"Hello"
is directly assigned to the arraystr
. The null terminator is automatically added by the compiler.
- Here, the string
Example for better understanding :
#include <stdio.h>
#include <string.h>
int main() {
// String declaration and initialization
char str1[12] = "hello";
char str2[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
char* str3 = "world";
// Print strings
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
printf("str3: %s\n", str3);
// String length
printf("Length of str1: %ld\n", strlen(str1));
// String concatenation
strcat(str1, " world");
printf("Concatenated string: %s\n", str1);
// String comparison
if (strcmp(str1, str3) == 0) {
printf("str1 and str3 are equal\n");
} else {
printf("str1 and str3 are not equal\n");
}
return 0;
}
Output:
str1: hello
str2: hello
str3: world
Length of str1: 5
Concatenated string: hello world
str1 and str3 are equal
Explanation:
Step1 : Header files
#include <stdio.h> // Standard input/output library for functions like printf
#include <string.h> // String library for functions like strlen, strcat, strcmp
#include <stdio.h>
: Includes the standard I/O library needed for functions likeprintf
to display output.#include <string.h>
: Includes the string handling library for manipulating and working with strings (e.g., getting string length, concatenation, comparison).
Step2: Main Function
int main() {
int main()
: The main function is the entry point of the C program. It returns an integer to indicate the success or failure of the program execution.
Step3: String Declaration and Initialization
char str1[12] = "hello"; // Declare str1 with size 12 to accommodate "hello" + " world"
char str2[6] = {'h', 'e', 'l', 'l', 'o', '\0'}; // Explicitly initialize str2 with characters
char* str3 = "world"; // Declare a pointer to the string literal "world"
char str1[12] = "hello";
: Declares a character arraystr1
of size 12, large enough to hold the string"hello"
(5 characters) and extra space to later concatenate" world"
.char str2[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
: Declares and explicitly initializes a character arraystr2
with individual characters, ensuring the null terminator ('\0'
) is added.char* str3 = "world";
: Declares a pointerstr3
that points to the string literal"world"
stored in memory.
Step4: Printing Strings
printf("str1: %s\n", str1); // Prints the content of str1, which is "hello"
printf("str2: %s\n", str2); // Prints the content of str2, which is also "hello"
printf("str3: %s\n", str3); // Prints the string literal "world"
%s
inprintf
is used to print strings. The function outputs the strings stored instr1
,str2
, and the string literal pointed to bystr3
.
Step5: String Length
printf("Length of str1: %ld\n", strlen(str1)); // Gets and prints the length of str1, which is 5
strlen(str1)
: Returns the length of the stringstr1
, which is"hello"
(5 characters). The function counts characters up to the null terminator (\0
).
Step6: String Concatenation:
strcat(str1, " world"); // Concatenates " world" to str1
printf("Concatenated string: %s\n", str1); // Prints the concatenated string
strcat(str1, " world");
: Concatenates" world"
to the existing string instr1
. The arraystr1
was declared with enough space (12 characters) to hold the combined result"hello world"
.
Step7: String Comparison:
if (strcmp(str1, str3) == 0) { // Compare str1 ("hello world") and str3 ("world")
printf("str1 and str3 are equal\n");
} else {
printf("str1 and str3 are not equal\n");
}
strcmp(str1, str3)
: Compares the two stringsstr1
("hello world"
) andstr3
("world"
). It returns0
if the strings are equal. Here, the strings are not equal, so theelse
block will execute.