
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Inserting elements in an array using C Language
We can insert the elements wherever we want, which means we can insert either at starting position or at the middle or at last or anywhere in the array.
After inserting the element in the array, the positions or index location is increased but it does not mean the size of the array is increasing.
The logic used to insert element is −
Enter the size of the array
Enter the position where you want to insert the element
Next enter the number that you want to insert in that position
for(i=size-1;i>=pos-1;i--) student[i+1]=student[i]; student[pos-1]= value;
Final array should be printed using for loop.
Program
#include<stdio.h> int main(){ int student[40],pos,i,size,value; printf("enter no of elements in array of students:"); scanf("%d",&size); printf("enter %d elements are:
",size); for(i=0;i<size;i++) scanf("%d",&student[i]); printf("enter the position where you want to insert the element:"); scanf("%d",&pos); printf("enter the value into that poition:"); scanf("%d",&value); for(i=size-1;i>=pos-1;i--) student[i+1]=student[i]; student[pos-1]= value; printf("final array after inserting the value is
"); for(i=0;i<=size;i++) printf("%d
",student[i]); return 0; }
Output
enter no of elements in array of students:6 enter 6 elements are: 12 23 34 45 56 67 enter the position where you want to insert the element:3 enter the value into that poition:48 final array after inserting the value is 12 23 48 34 45 56 67
- Related Articles
- What are the inserting elements in queue in C language?
- How to calculate sum of array elements using pointers in C language?
- Equalize an array using array elements only in C++
- C++ program to find array after inserting new elements where any two elements difference is in array
- Print sorted distinct elements of array in C language
- Rank of All Elements in an Array using C++
- How to pass individual elements in an array as argument to function in C language?
- How to find minimum element in an array using linear search in C language?
- How to find minimum element in an array using binary search in C language?
- Explain insertion of elements in linked list using C language
- How to access an array element in C language?
- What is an array of structures in C language?
- Inserting element at falsy index in an array - JavaScript
- C++ Program to Access Elements of an Array Using Pointer
- Inserting Elements to a doubly linked list using Javascript

Advertisements