
Arrays in C are the collections of variables of the same data type stored in a contiguous memory location. They allow us to store large amounts of data efficiently in a structured way. They are accessed using an index.

Arrays in C are classified into two types i.e single-dimensional Array and multi-dimensional arrays. A single-dimensional array is a collection of values of the same data type, whereas a two-dimensional array represents a mathematical matrix. We will look at both types in detail.
Single Dimensional Array
A single-dimensional array is a collection of values of the same data type. They are stored in contiguous memory locations and can be accessed using an index. We will look at the example of a single-dimensional array.
NOTE: Single Dimensional array is used to store and manipulate lists or sequences of data. They can also be used in implementation of data structures such as stacks, queues and linked lists.
Declaring Single-Dimensional Array
There are multiple ways in which we can declare a single dimensional array. Some of them are:
Declaration with Fixed Size
The simplest way to declare the array is to specify its size at compile time. First, let’s look at the syntax for declaring an array with fixed size.
data_type array_name[array_size];
- where ‘data_type’ is the type of the data, such as int, float.
- ‘array_name’ is the name of the array you wish to give.
- ‘array_size’ is the size of the array.
For example, to declare an array of 10 integers we would write:
int arr[10];
Declaration with Initializer
We can also initialize an array at the time of declaration using an initializer list.
The syntax for an declaring an array with initializer is:
data_type array_name[] = {val1, val2, …, valN};
- where ‘data_type’ is the type of the data such as int, float.
- ‘array_name’ is the name of the array you wish to give.
- ‘val1’,..., ‘valN’ are the values to be given.
Now let’s look at an example of an array declared with initializer:
int arr[] = {1, 2, 3, 4, 5};
Dynamic Declaration in C
We can use malloc() at runtime to dynamically allocate memory for an array. The malloc() function is commonly used for dynamic allocation in C, and it returns a pointer to a block of memory of the specified size. It is important to remember to free dynamically allocated memory using the free() function when it is no longer needed, to avoid memory leaks.
Let’s look at the syntax for dynamic declaration in C:
data_type *array_name = (data_type*)malloc(array_size * sizeof(data_type));
- where ‘data_type’ is the type of the data such as int, float.
- ‘array_name’ is the name of the array you wish to give.
- ‘(data_type*)malloc(array_size * sizeof(data_type))’ this is the dynamic allocation statement, malloc is used to allocate a block of memory of specified size in bytes. sizeof is used to calculate the size required for each element based on the data type.
For example to declare an array of 10 integers dynamically. We would write:
int *arr = (int*)malloc(10 * sizeof(int));
Example
Now we will do some operations on an array. Let's learn to modify the 2nd element of the array.
Later we will try to print all the elements of the array.
Output

Multi Dimensional Array
A multi-dimensional array is a collection of values of the same data type. Multi Dimensional array is used in analysing and processing data with multiple dimensions, such as performing simulations on a grid.
Two Dimensional Array
2-D arrays are the most used in multi-dimensional arrays. These are generally stored in the form of rows and columns and are used to represent matrices.
Let's see how to declare 2-dimensional array in C.
Declaration with Fixed Size
The simplest way to declare the array is to specify its size at compile time.
Let’s look at the syntax for declaring a multidimensional array with fixed size:
data_type array_name[rows][columns];
- where ‘data_type’ is the type of the data such as int, float.
- ‘array_name’ is the name of the array you wish to give.
- and rows and columns are the sizes of rows and columns.
For example to declare a 2D array with 4 rows and 5 columns we use:
int arr[4][5];
Declaration with Initializer
We can also initialize an array at the time of declaration using an initializer list.
The syntax for an declaring a multidimensional array with initializer is:
data_type array_name[size1][size2] = {{value11, value12, ..., value1N}, {value21, value22, ..., value2N}, ..., {valueM1, valueM2, ..., valueMN}};
- where ‘data_type’ is the type of the data such as int, float.
- ‘array_name’ is the name of the array you wish to give.
- ‘size1’ and ‘size2’ are the sizes of the rows and columns respectively.
- ‘value11’, …,'value1N' are the values of the first row of the array.
- Similarly ‘valueM1’, …,'valueMN' are the values of the Mth row of the array.
For example to declare a 2D array with 2 rows and 3 columns and initialize it with values.
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
Dynamic Declaration in C
As we have done for single Dimensional array. We can use malloc() at runtime to dynamically allocate memory for a multi-dimensional array.
Let’s look at the syntax for dynamic declaration in C:
data_type **array_name = (data_type**)malloc(rows * sizeof(data_type*));
for (int i = 0; i < rows; i++) {
array_name[i] = (data_type*)malloc(columns * sizeof(data_type));
}
- where ‘data_type’ is the type of the data such as int, float.
- ‘array_name’ is the name of the array you wish to give.
- ‘(data_type**)malloc(rows * sizeof(data_type*))’ this is the dynamic allocation statement, malloc is used to allocate a block of memory of specified size in bytes. sizeof is used to calculate the size required for each element based on the data type.
- The ‘for’ loop is used to declare the elements in the 2D array.
For example to declare a 2D array with 2 rows and 3 columns dynamically we use
int **arr = (int**)malloc(2 * sizeof(int*));
for (int i = 0; i < 2; i++) {
arr[i] = (int*)malloc(3 * sizeof(int));
}
Example
Now let’s look at an example of a 2-dimensional array and perform some operations on it.
- We will initialise a two-dimensional matrix of size 3*3.
- First, we will try to access the element matrix[0][0], which is 10.
- Next, we will change the element in the second row and the third column using matrix[1][2] to 100.
-
Later we will print the matrix elements using two for loops.
Output

Three Dimensional Array
Three dimensional arrays are special forms of multidimensional arrays which are used to represent three dimensional coordinates in programming language. It is an extension of the concept of two dimensional array where each element is accessed using three dimensions instead of two.
The syntax for a three dimensional array is:
dataType arrayName[size1][size2][size3];
Here, data type represents the type of data such as ‘int’ or ‘float’. ‘size1’, ‘size2’ and ‘size3’ represent the sizes of the array in three dimensions.
Example
Now we will look at an example of a three-dimensional array. We will initialize a three-dimensional array and print that array using three for loops.
Output

Also see, Short int in C Programming