What is an array in C?

An Array is a group of similar types of elements. It has a contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

  • One-dimensional array: One-dimensional array is an array that stores the elements one after the another.

Syntax:

  1. data_type array_name[size];
  • Multidimensional array: Multidimensional array is an array that contains more than one array.

Syntax:

  1. data_type array_name[size];

Example of an array:


int main()  
{  
   int arr[5]={1,2,3,4,5}; //an array consists of five integer values.  
   for(int i=0;i<5;i++)  
   {  
       printf(“%d “,arr[i]);  
   }  
    return 0;  
}  

Output:

1 2 3 4 5