Program to insert element in C array

Let’s learn how to insert element in array using C programming language. Consider an array of integer which has a size of 10 and have 6 element inside it.

int my_array[10] = {3, 5, 6, 9, 4, 8};

Now we want to insert a new element which is 7 at the index zero of this array. So, the final array will be as follows;

int my_array[10] = {7, 3, 5, 6, 9, 4, 8};

This is called insertion of a new element in an array without increasing its size. You may have noticed that the index of every other element has increased by one after inserting the new element.

In this article we will see C program to insert an element in array. Let’s see the program bellow;

C program to insert an element in array

// c program to insert an element in array

#include <stdio.h>
int main(){
   int Num, k, main_array[30], ins_psn, new_element;
   printf("Enter how many element in the array : ");
   scanf("%d", &Num);

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

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

   printf("\nEnter the insertion position : ");
   scanf("%d", &ins_psn);

   printf("\nEnter the new element which you want to insert : ");
   scanf("%d", &new_element);

   for(k = Num - 1; k >= ins_psn - 1; k--){
      main_array[k + 1] = main_array[k];
   }
   main_array[ins_psn - 1] = new_element;

   printf("\nNew array after inserting %d is : \n", new_element);

   for(k = 0; k <= Num; k++){
      printf("%d ", main_array[k]);
   }
   return 0;
}

Output of insert element program :

insert-an-element-in-array-c-program-to-insert-an-element-in-array

You can make the same program using function and recursion. Try this in your own. In the next page we will see the program to delete an element form an array. Simply click on next to go to that page.

Previous pageMerge two array

Next pageDelete element from array