Array In C
In C, array is a collection of variables that are stored in a continuous
memory of same data type.
It can be accessed by common name and index number.
Syntax:
data_type array_name[array_size];
Example:
int myArray[9];
Types of array in C:
There are two main types of array used in C programming language:
- One-dimensional array
- Multi-dimensional array
One-dimensional array:
- One dimensional array is also called as single dimensional array.
- It is a collection of same data type elements which are stored in a continuous memory location.
- An array's index, which starts at 0 and goes up to the array's size minus 1, is used to access its elements.
Program:
#include <stdio.h>
int main()
{
int arr[8] = {1, 2, 3, 4, 5,6,7,8};
int i;
printf(" values: ");
for (i = 0; i < 8; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return
0;
}
int main()
{
int arr[8] = {1, 2, 3, 4, 5,6,7,8};
int i;
printf(" values: ");
for (i = 0; i < 8; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return
0;
}
Output:
Values: 1 2 3 4 5 6 7 8
Multi-dimensional array:
- An array with more than one dimension is referred to as a multi-dimensional array.
- It is a collection of array,the two dimensional array is the most basic multi-dimensional array.
Program:
#include <stdio.h>
int main()
{
int arr[3][4] = {{11, 12, 13, 14}, {15, 16,
17, 18}, {19, 20, 21, 22}};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
11 12 13 14
15 16 17 18
19 20 21 22