Summary: In this tutorial, we will learn to create an array of strings in C programming language.

C language has no ‘string’ datatype, so we usually store a string value as a set of characters in a char array.

The following is an example of how exactly a string is stored in C:

char name[30] = "Pencil";

The above char array can store only one string value. How can we store multiples string values in C?

We can do so by creating an array of char array, like the following:

char names[size][30];       // 2D Array

The above statement creates a string array which can store up to ‘size’ number of elements, each of length 30.

Here’s a C program, in which we’re storing 3 strings in an array and output the same:

#include <stdio.h>
#include <stdlib.h>
 
//Function to display Array
void display(char arry[][30], int size){
    int i;
 
    printf("\n");
    for(i=0; i<size; i++){
        printf("%s ",arry[i]);
    }
    printf("\n");
}
 
int main()
{
    int i, size;
 
    printf("Enter size of the array \n");
    scanf("%d",&size);
 
    char arry[size][30];
 
    //Inputting strings into the array
    printf("Enter %d strings \n",size);
    for(i=0; i<size; i++){
        scanf("%s", arry[i]);
    }
 
    //Displaying Array
    printf("-------------------------------------------\n");
    printf("\t Array \n");
    printf("-------------------------------------------\n");
    display(arry, size);
 
}

Output:

String Array in C

In this tutorial, we learned to create an array of strings in the C programming language. Comment below if you know any other methods of storing strings in C.

Leave a Reply