Program to delete an element from C array

Here, we will see C program to delete element from array. Suppose, we have an integer type array of size 10 having 7 element which has the value assigned in it like as bellow;

int main_array[10] = {3, 4, 7, 8, 9, 11, 6};

Now we want to delete or remove an element from this array. Assume, we will remove the element from position three which is 7. So, the new array after deletion will looks like this;

int main_array[10] = {3, 4, 8, 9, 11, 6};

You may have noticed that every element which has the position after the deleted element has reduced their index by one. The total size of array remains same after deleting any element.

However, you can delete any element from an array. In this guide we will write a C program to delete an element from C array. Look at the following program.

C program to delete an element from array

// delete element from array in c

#include <stdio.h>
int main(){
   int Num, x, main_array[30], del_posn;

   printf("Enter how many integers in the array : ");
   scanf("%d", &Num);

   printf("\nEnter all array element here : \n");

   for(x = 0; x < Num; x++){
      scanf("%d", &main_array[x]);
   }

   printf("\nEnter the position from where you want to delete element : ");
   scanf("%d", &del_posn);

   if(del_posn >= Num + 1){
      printf("\nValue not exist in the array.\n");

   }else{
      for(x = del_posn - 1; x < Num - 1; x++){
         main_array[x] = main_array[x + 1];
      }

      printf("\nArray after deletion is : \n");

      for(x = 0; x < Num - 1; x++){
         printf("%d ", main_array[x]);
      }
   }

   return 0;
}

Output of delete element program:

delete-element-from-array-c-program-to-delete-an-element-from-array

You can delete an element from an array using this process. It may be any type of array like integer, float, double or even character.

However, it is not recommended to follow this process of deletion of an element if the array size is large and need to shift many element. If we need to delete element frequently then it is not recommended at all. For that case we should use linked list data structure.

Previous pageInsert element in array

Next pageAdd two matrix