C Array

An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number.

Declaration of C Array

We can declare an array in the c language in the following way.

  1. data_type array_name[array_size];  

Now, let us see the example to declare the array.

  1. int marks[5];  

Here, int is the data_type, marks are the array_name, and 5 is the array_size.

Initialization of C Array

The simplest way to initialize an array is by using the index of each element. We can initialize each element of the array by using the index. Consider the following example.

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

initialization of array in c language

C array example

  1. #include<stdio.h>  
  2. int main(){      
  3. int i=0;    
  4. int marks[5];//declaration of array       
  5. marks[0]=80;//initialization of array    
  6. marks[1]=60;    
  7. marks[2]=70;    
  8. marks[3]=85;    
  9. marks[4]=75;    
  10. //traversal of array    
  11. for(i=0;i<5;i++){      
  12. printf("%d \n",marks[i]);    
  13. }//end of for loop     
  14. return 0;  
  15. }    

Output

80
60
70
85
75

C Array: Declaration with Initialization

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

  1. int marks[5]={20,30,40,50,60};  

In such case, there is no requirement to define the size. So it may also be written as the following code.

  1. int marks[]={20,30,40,50,60};  

Let's see the C program to declare and initialize the array in C.

  1. #include<stdio.h>  
  2. int main(){      
  3. int i=0;    
  4. int marks[5]={20,30,40,50,60};//declaration and initialization of array    
  5.  //traversal of array    
  6. for(i=0;i<5;i++){      
  7. printf("%d \n",marks[i]);    
  8. }    
  9. return 0;  
  10. }    

Output

20
30
40
50
60

C Array Example: Sorting an array

In the following program, we are using the bubble sort method to sort the array in ascending order.

  1. #include<stdio.h>    
  2. void main ()    
  3. {    
  4.     int i, j,temp;     
  5.     int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};     
  6.     for(i = 0; i<10; i++)    
  7.     {    
  8.         for(j = i+1; j<10; j++)    
  9.         {    
  10.             if(a[j] > a[i])    
  11.             {    
  12.                 temp = a[i];    
  13.                 a[i] = a[j];    
  14.                 a[j] = temp;     
  15.             }     
  16.         }     
  17.     }     
  18.     printf("Printing Sorted Element List ...\n");    
  19.     for(i = 0; i<10; i++)    
  20.     {    
  21.         printf("%d\n",a[i]);    
  22.     }