Array

Array in C language is a collection or group of elements (data). All the elements of c array are homogeneous (similar). It has contiguous memory location..
C array is beneficial if you have to store similar elements. Suppose you have to store marks of 50 students, one way to do this is allotting 50 variables. So it will be typical and hard to manage. For example we can not access the value of these variables with only 1 or 2 lines of code.
Another way to do this is array. By using array, we can access the elements easily. Only few lines of code is required to access the elements of array.

Advantage of C Array

1) Code Optimization: Less code to the access the data.
2) Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.
3) Easy to sort data: To sort the elements of array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.

Declaration of C Array

We can declare an array in the c language in the following way.
data_type array_name[array_size];
Now, let us see the example to declare array.
int marks[5];
Here, int is the data_type, marks is the array_name and 5 is the array_size.

Initialization of C Array

A simple way to initialize array is by index. Notice that array index starts from 0 and ends with [SIZE - 1]. because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.
marks[0]=70;  //initialization of array
marks[1]=80;
marks[2]=90;
marks[3]=75;
marks[4]=95;

Let's try to understand the concept of call by reference in c language by the example given below:

C array example

#include <stdio.h>   
void main(){
int i=0;
int marks[5]; //declaration of array
marks[0]=70; //initialization of array
marks[1]=80;
marks[2]=90;
marks[3]=75;
marks[4]=95;
//traversal of array
for(i=0;i<5;i++){
printf(" %d",marks[i]);
}   //end of for loop
}
Output: 70 80 90 75 95  

Declaration with Initialization

We can initialize the c array at the time of declaration. Let's see the code.

1. int marks[5] = {10,20,30,70,80};
In such case, there is no requirement to define size. So it can also be written as the following code.
2. int marks[ ] = {10,20,30,70,80} ;
Let's see the full program to declare and initialize the array in C.
#include <stdio.h>  
void main(){
 int i=0;
 int marks[5]={10,20,30,70,80};  //declaration and initialization of array
 for(i=0;i<5;i++){
 printf("  %d ",marks[i]);
     }
	 }
Output: 10 20 30 70 80