In this tutorial you will learn about the C Program to Delete an Element from an Array and its application with practical example.
C Program to Delete an Element from an Array
In this tutorial, we will learn to create a C program that will delete the element of Array using C programming.
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
Delete the element of Array
As we all know array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First will take the number of elements of an array from the user. Then will take the elements from the user for the array. And at last, we will delete the array element by asking the user with the help of C Programming Language.
Step 1. Initialize variable.
Step 2. Take size of array .
Step 3. Take elements of array.
Step 4. Take location to delete a element.
Step 5. Delete the element.
Step 6. Print result.
Step 7. End
To Delete an element from the array
//Declaring the variables
int x, loc, i, n;
//taking size of array
printf("Enter number of elements in array\n");
//taking elements of array
printf("Enter %d elements for the array \n", n);
//for loop for input
for (i = 0; i < n; i++)
//taking deletion position i.e. location for deleting element
printf("Enter the location where you wish to delete element\n");
if (loc >= n+1)
printf("Deletion not possible.\n");
for (i = loc - 1; i < n - 1; i++)
x[i] = x[i+1];
for (i = 0; i < n - 1; i++)
In the above program, we have first initialized the required variable.
- x = it will hold the elements in an array.
- n = it will hold the number of elements in an array.
- i = it will hold the integer value to control the array.
- loc = it will hold the location for delete element size of the array.
Taking input from the user in an array number of elements in the array
Taking input from the user in array elements in the array