Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Insert an Element in an Array
Next article icon

C Program to Insert an Element in an Array

Last Updated : 16 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to insert an element into an array in C.

C arrays have a fixed size, so we cannot dynamically increase their memory. However, we can insert an element if the array already have enough memory space to accommodate the new elements

Insert Element at Specific Position

To insert an element at a specific position, shift all elements to the right of the specified position one step to the right. This will create an empty space at the specified position. Then, insert the new element at the desired position.

C
#include <stdio.h>  void insert(int arr[], int *n, int pos, int val) {        // Shift elements to the right     for (int i = *n; i > pos; i--)         arr[i] = arr[i - 1];      // Insert val at the specified position     arr[pos] = val;      // Increase the current size     (*n)++; }  int main() {     int arr[7] = {10, 20, 30, 40, 50};     int n = 5;     int pos = 3;     int val = 25;      // Insert the value at the specified position     insert(arr, &n, pos, val);      for (int i = 0; i < n; i++)         printf("%d ", arr[i]);     return 0; } 

Output
10 20 30 25 40 50 

Explanation: In the given program, the function insert() shifts all elements starting from the insertion index 3 one step to the right. The new value 25 is then inserted at the desired position, and the size of the array is incremented.

Insert-Element-at-specific-position-in-C-array

Time Complexity: O(n) (for Shifting) + O(1) (for incrementing size) = O(n)
Auxiliary Space: O(1)

If you want to explore array manipulation and other data structures, the C Programming Course Online with Data Structures provides hands-on examples and exercises.

Insert an Element at the End of an Array

If you are inserting an element at the end, there's no need for shifting elements. You can directly add the element at the next available position and increase the size.

C
#include <stdio.h>  void insertLast(int arr[], int *n, int val) {      // Insert val at last     arr[*n] = val;      // Increase the current size     (*n)++; }  int main() {     int arr[7] = {10, 20, 30, 40, 50};     int n = 5;     int val = 25;      // Insert the value at the end     insertLast(arr, &n, val);      for (int i = 0; i < n; i++)         printf("%d ", arr[i]);     return 0; } 

Output
10 20 30 40 50 25 
Insert-Element-at-the-end-in-C-array

Time Complexity: O(1) (for decrementing size)
Auxiliary Space: O(1)

Note: Apart from insertion at the end, all cases will be handled same as insertion at specific position. Also, make sure array have enough space to avoid segmentation faults.


Next Article
C Program to Insert an Element in an Array

C

code_r
Improve
Article Tags :
  • C Language
  • c-array
  • C-Arrays

Similar Reads

    How to store words in an array in C?
    We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
    2 min read
    Implement a Stack in C Programming
    Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate
    7 min read
    How to declare a Two Dimensional Array of pointers in C?
    A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo
    3 min read
    Pass Array to Functions in C
    Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
    3 min read
    Pointer to an Array | Array Pointer
    A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences