An array in C is a collection of elements having the same data type. It also defines the continuous memory location of the variable and which can be referred to as a single variable.
Declaration of an Array
type variable_name[];
In the above example, type denotes the data type of the array and variable_name denotes the name of the array.
Examples of some basic Array of specific data type
int a[10];
float a[20];
double a[20];
char a[20];
How to declare and Initialize an Array in C
int a[5]={8,56,34,57,78};
int a[]= {3,5,6,8,9};
float a[]={13.5,23.7,45.12,-34.4,-12.8};
char a1[7]="Sumit";
Some Important points about array by taking the above example
- All array elements would be present in contiguous memory Locations.
- 4 bytes for the integer variable. If an array has 6 elements then it consumes total of 24 bytes of memory.
- Arrays indexing always starts from a[0].
- In the above Example of a C array, each array occupies indexes from a[0] to a[5].
In addition, below We have also mentioned some properties of an array. So please have a look over it.
Properties of An Array in C Program
An Array has the following properties
- Elements of an array should be of a similar data type.
- It takes memory a contiguous fashion.
- Array elements are stored in a continuous fashion so that they can be randomly accessed.
Now together with properties let’s have an eye on the advantages and disadvantages of an Array.
Advantage of C Array in C Program
1) Code Optimization: Data can be accessed with very less code.
2) Ease of traversing: Traversing of an Array is very easy with for loop.
3) Ease of sorting: We can easily sort Array elements.
4) Random Access: Also we can access elements randomly.
5) Data Structure Implementation: Array helps to implement various data structures like linked lists, stacks, queues, trees, graphs, etc.
6) Matrix Representation: With the help of 2 Array, Matrix can be represented.
Disadvantage of C Array
1). Time complexity: In insertion and deletion operations, Time complexity increases.
2). Memory Wastage: Because arrays are fixed in size so if no elements in an array, still it consumes all spaces.
3). Fixed In Size: Once you had declared the size of an array, it is not possible to increase it.
Array example in C
#include <stdio.h>
int main()
{
int a=0;
int marks[4];
marks[0]=60;
marks[1]=10;
marks[2]=44;
marks[3]=8;
for(a=0;a<4;a++){
printf("%d \n",marks[a]);
}
return 0;
}
Output
60
10
44
8
Types of Array in C
There are 2 types of C arrays
- One dimensional array
- Multi dimensional array
- Two dimensional array
- Three dimensional array
- four dimensional array etc….
One Dimensional Array
This can be considered as array as a row, where elements are stored one after another.
Syntax
data-type arr_name[array_size];
data-type: It denotes the type of the elements in the array.
arr_name: Name of the array. It must be a valid identifier.
array_size: Number of elements an array can hold.
Example 1: 1D Array implementation In C
#include <stdio.h>
int main()
{
int a=0;
int marks[4];
marks[0]=10;
marks[1]=20;
marks[2]=30;
marks[3]=40;
marks[4]=50;
for(a=0;a<5;a++){
printf("%d \n",marks[a]);
}
return 0;
}
Output
10
20
30
40
50
Example 2: 2-D Array implementation In C
#include <stdio.h>
int main()
{
int i,j;
int arr[2][2] = {10,20,30,40};
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
return 0;
}
Output
value of arr[0] [0] : 10
value of arr[0] [1] : 20
value of arr[1] [0] : 30
value of arr[1] [1] : 40
Example 3: 2-D Array implementation In C
#include <stdio.h>
int main()
{
int i,j,arr[2][2];
arr[0][0] = 10;
arr[0][1] = 20;
arr[1][0] = 30;
arr[1][1] = 40;
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
{
printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
}
}
return 0;
}
Output
value of arr[0] [0] : 10
value of arr[0] [1] : 20
value of arr[1] [0] : 30
value of arr[1] [1] : 40