In C programming language, a is an array of character sequences terminated by NULL, it is a one-dimensional array of characters. And, the array of strings is an array of strings (character array).
What is an Array of Strings in C?
Thus, an array of strings can be defined as –
An array of strings is a two-dimensional array of character-type arrays where each character array (string) is null-terminated.
To declare a string, we use the statement −
char string[] = {''H'', ''e'', ''l'', ''l'', ''o'', '' ''}; Or char string = "Hello";
Declare and Initialize an Array of Strings
To declare an array of strings, you need to declare a of character types, where the first subscript is the total number of strings and the second subscript is the maximum size of each string.
To initialize an array of strings, you need to provide the multiple strings inside the double quotes separated by the commas.
Syntax
To construct an array of strings, the following syntax is used −
char strings [no_of_strings] [max_size_of_each_string];
Example
Let us declare and initialize an array of strings to store the names of 10 computer languages, each with the maximum length of 15 characters.
char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" };
Printing An Array of Strings
A string can be printed using the with %s format specifier. To print each string of an array of strings, you can use the till the number of strings.
Example
In the following example, we are declaring, initializing, and printing an array of string −
#include <stdio.h> int main (){ char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" }; for (int i = 0; i < 10; i++){ printf("%sn", langs[i]); } return 0; }
Output
When you run this code, it will produce the following output −
PYTHON JAVASCRIPT PHP NODE JS HTML KOTLIN C++ REACT JS RUST VBSCRIPT
Note: The size of each string is not equal to the row size in the declaration of the array. The “