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:

  1. We declare an array of string pointers fruits[], where each element is a string.
  2. The sizeof(fruits) / sizeof(fruits[0]) calculates the total number of strings.
  3. We use a for loop to iterate from index 0 to size - 1.
  4. 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:

  1. We declare an array of string pointers colors[] with 5 color names.
  2. We calculate the number of elements using sizeof(colors) / sizeof(colors[0]).
  3. We initialize i = 0 and use a while loop.
  4. The loop continues as long as i is less than size.
  5. Inside the loop, printf("%s\n", colors[i]) prints each string, and we increment i.

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:

  1. We declare an array of string pointers animals[] containing 5 animal names.
  2. We calculate the number of elements using sizeof(animals) / sizeof(animals[0]).
  3. We initialize i = 0 and use a do-while loop.
  4. The do block prints the value at index i using printf("%s\n", animals[i]), then increments i.
  5. 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:

  1. for Loop: Iterates through the array using an index counter.
  2. while Loop: Uses a condition to iterate until reaching the last element.
  3. do-while Loop: Ensures the loop runs at least once.