# Arrays in C programming

In this tutorial, we will learn about the arrays in C programming and ways to declare, initialize and access it.

An array is a collection of data items, all of the same type and having fixed size. We can represent array in n-dimension. For example ,

- One dimensional
- Two dimensional
- ………
- n dimensional

## Array declaration in C

We declare an array by specifying its data_type, size and array_name.

`data_type array_name[array_size];`

#### Example

`int student[10]`

In above example, we have declared array student of type Integer with size 5. i.e It can hold maximum 5 in values.

## One dimensional array in C

A one-dimensional array is like a list.

`int mark[10]`

Here, student can be considered as a list with single column/row.

Here, we can access the block with the name of array and its position.

## Two dimensional array in C

Two dimensional array are like Table.

`int a[2][3]`

Here,The array y can hold 12 elements.

Similarly, you can declare a three-dimensional (3d) array. For example,

`float y[2][4][3];`

Here,The array y can hold 24 elements.

You can think this example as: Each 2 elements have 4 elements, which makes 8 elements and each 8 elements can have 3 elements. Hence, the total number of elements is 24.

## Initializing Arrays

Arrays are initialize in the same way any variable is done. The data assign to array is placed in curly braces {}. Data in the curly braces are separated using comma ( , ).

`int array[ 6 ] = { 1, 2, 3, 4, 5, 6 }`

Here the data to be initialized is placed inside curly braces and separted by comma.

If the number of values in the list is less than the size of the array, the remaining elements of the array are initialized to zero.

If the array is initialized more than its size it is an error. For example :

`int array[ 6 ] = { 1, 2, 3, 4, 5, 6,7 , 8 } //error`

Here, the size of array is defined 6. i.e it can hold at max 6 elements but it is initialized 8 elements.

## Ways of initializing arrays in C

```
// Different ways to initialize two dimensional array
int c[2][3] = {{2, 9, 0}, {8, 5, 2}};
int c[][3] = {{2, 9, 0}, {8, 5, 2}};
int c[2][3] = {2, 9, 0, 8, 5, 2};
```

#### Example **to find the sum of two matrices using multidimensional arrays.**

```
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
int i, j;
// Taking input using nested for loop
printf("Enter elements of 1st matrix\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("Enter a%d%d: ", i+1, j+1);
scanf("%f", &a[i][j]);
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrix\n");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("Enter b%d%d: ", i+1, j+1);
scanf("%f", &b[i][j]);
}
// adding corresponding elements of two arrays
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
// Displaying the sum
printf("\nSum Of Matrix:");
for(i=0; i<2; ++i)
for(j=0; j<2; ++j)
{
printf("%.1f\t", c[i][j]);
if(j==1)
printf("\n");
}
return 0;
}
```

#### Output

```
Enter elements of 1st matrix
Enter a11: 2;
Enter a12: 0.5;
Enter a21: -1.1;
Enter a22: 2;
Enter elements of 2nd matrix
Enter b11: 0.2;
Enter b12: 0;
Enter b21: 0.23;
Enter b22: 23;
Sum Of Matrix:
2.2 0.5
-0.9 25.0
```