C Array

Array in C language is a collection or group of elements . All the elements of c array are similar. It has contiguous memory location.

C array is beneficial if you have to store similar elements. Suppose you have to store marks of 400 students, one way to do this is allotting 400 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.

Advantages of C Array

  • Code Optimization:
  • Easy to traverse data:
  • Easy to sort data
  • Random Access

Disadvantage of C Array

Whatever size, we define at the time of declaration of array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.

Declaration of C Array

data_typearray_name[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].

  1. marks[0]=80;//initialization of array
  2. marks[1]=60;
  3. marks[2]=70;
  4. marks[3]=85;
  5. marks[4]=75;


C Array Example


 #include<stdio.h>
#include<conio.h>
void main(){ 
inti=0; 
int marks[5];//declaration of array
clrscr(); 
marks[0]=80;//initialization of array
marks[1]=60; 
marks[2]=70; 
marks[3]=85; 
marks[4]=75; 



//traversal of array
for(i=0;i<5;i++){ 
printf("%d \n",marks[i]); 
}//end of for loop

getch(); 
} 

				

Output:

80
60
70
85
75