Print a String Array in C
To print a string array in C, we need to iterate over each string in the array using a loop and use the printf()
function to display it. Since C does not have a built-in string type, we use character arrays (char arrays) or pointers to store multiple strings.
Examples to Print a String Array
1. Printing a String Array Using a for
Loop
In this example, we declare an array of strings and use a for
loop to print each string.
main.c
</>
Copy
#include <stdio.h>
int main() {
// Declare an array of strings
char *fruits[] = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
int size = sizeof(fruits) / sizeof(fruits[0]);
// Print each string using a for loop
for (int i = 0; i < size; i++) {
printf("%s\n", fruits[i]);
}
return 0;
}
Explanation:
- We declare an array of string pointers
fruits[]
, where each element is a string. - The
sizeof(fruits) / sizeof(fruits[0])
calculates the total number of strings. - We use a
for
loop to iterate from index0
tosize - 1
. - Inside the loop,
printf("%s\n", fruits[i])
prints each string on a new line.
Output:
Apple
Banana
Cherry
Date
Elderberry
2. Printing a String Array Using a while
Loop
In this example, we use a while
loop to print all the strings from an array.
main.c
</>
Copy
#include <stdio.h>
int main() {
// Declare an array of strings
char *colors[] = {"Red", "Green", "Blue", "Yellow", "Purple"};
int size = sizeof(colors) / sizeof(colors[0]);
int i = 0;
// Print each string using a while loop
while (i < size) {
printf("%s\n", colors[i]);
i++;
}
return 0;
}
Explanation:
- We declare an array of string pointers
colors[]
with 5 color names. - We calculate the number of elements using
sizeof(colors) / sizeof(colors[0])
. - We initialize
i = 0
and use awhile
loop. - The loop continues as long as
i
is less thansize
. - Inside the loop,
printf("%s\n", colors[i])
prints each string, and we incrementi
.
Output:
Red
Green
Blue
Yellow
Purple
3. Printing a String Array Using a do-while
Loop
In this example, we use a do-while
loop to print a list of animals.
main.c
</>
Copy
#include <stdio.h>
int main() {
// Declare an array of strings
char *animals[] = {"Dog", "Cat", "Elephant", "Lion", "Tiger"};
int size = sizeof(animals) / sizeof(animals[0]);
int i = 0;
// Print each string using a do-while loop
do {
printf("%s\n", animals[i]);
i++;
} while (i < size);
return 0;
}
Explanation:
- We declare an array of string pointers
animals[]
containing 5 animal names. - We calculate the number of elements using
sizeof(animals) / sizeof(animals[0])
. - We initialize
i = 0
and use ado-while
loop. - The
do
block prints the value at indexi
usingprintf("%s\n", animals[i])
, then incrementsi
. - The loop condition
i < size
is checked after execution, ensuring at least one iteration.
Output:
Dog
Cat
Elephant
Lion
Tiger
Conclusion
In this tutorial, we explored different ways to print a string array in C:
for
Loop: Iterates through the array using an index counter.while
Loop: Uses a condition to iterate until reaching the last element.do-while
Loop: Ensures the loop runs at least once.