C program to delete an element from an array
Posted by Samath
Last Updated: January 02, 2017

Write a C program to delete an element from an array at specified position. Logic to remove an element from any given position in an array. The program should also print an error message if the delete position is invalid.


#include <stdio.h>
int main()
    int arr[100];
    int i, n, position;
     * Reads size and elements in array from user
    printf("Enter size of the array : ");
    scanf("%d", &n);
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
        scanf("%d", &arr[i]);
    // Reads the position to be deleted from user
    printf("Enter the element position to delete : ");
    scanf("%d", &position);
    // If delete position is invalid
    if(position==n+1 || position<0)
        printf("Invalid position! Please enter position between 1 to %d", n);
        for(i=position-1; i<n-1; i++)
            // Copy next element value to current element
            arr[i] = arr[i+1];
        // Decrement the size by 1
    // Prints the array after delete operation
    printf("\nElements of array after delete are : ");
    for(i=0; i<n; i++)
        printf("%d\t", arr[i]);
    return 0;