8.10.25

Creating Array in C







Creating Arrays in C: Create, Insert, Delete & Display

Arrays are one of the most fundamental concepts in C programming. If you’ve ever tried to store multiple values like a list of favorite numbers  you’ve already needed arrays! Let’s explore how they work and what’s happening in this simple C program.

What is an Array?



An array is like a bookshelf it has a fixed number of slots (or compartments), and each slot can hold one book (a value).

In C, we can declare an array using:

data_type array_name[size];

Example:

int numbers[5];

This creates a space for 5 integers in memory all next to each other!

The Program

#include <stdio.h>

int main() {
  int favorite_numbers[5]; // Declaration of an array of 5 integers

  // Creating (assigning values)
  favorite_numbers[0] = 100;
  favorite_numbers[1] = 99;
  favorite_numbers[2] = 5;
  favorite_numbers[3] = 23;
  favorite_numbers[4] = 7;

  // Displaying values
  printf("My favorite numbers are %d\n%d\n%d\n%d\n%d\n", 
         favorite_numbers[0],
         favorite_numbers[1],
         favorite_numbers[2],
         favorite_numbers[3],
         favorite_numbers[4]);

  return 0;
}

Explanation:

1. Declaration

int favorite_numbers[5];

Here, we tell the compiler to reserve space for 5 integers in memory.
Each integer gets its own index — from 0 to 4.

Index Value
0
1
2
3
4

2. Creation (Assigning Values)

We now assign each books (array element) a value:

favorite_numbers[0] = 100;
favorite_numbers[1] = 99;
favorite_numbers[2] = 5;
favorite_numbers[3] = 23;
favorite_numbers[4] = 7;

Index Value
0 100
1 99
2 5
3 23
4 7

3. Display (Printing)

We can display the array elements one by one:


printf("My favorite numbers are %d\n%d\n%d\n%d\n%d\n",
       favorite_numbers[0],
       favorite_numbers[1],
       favorite_numbers[2],
       favorite_numbers[3],
       favorite_numbers[4]);

Output:

My favorite numbers are 100
99
5
23
7

You can use a Loop for Display

Instead of writing multiple printfs, we can use a loop:


for (int i = 0; i < 5; i++) {
    printf("%d\n", favorite_numbers[i]);
}

This way, if you ever change the size of the array, the loop still works perfectly!

Deletion and Insertion? 

Arrays in C have a fixed size, meaning you can’t directly “delete” or “insert” new elements but you can shift values.

Example: Deleting 99
You can simply move all elements after 99 one step back:


for (int i = 1; i < 4; i++) {
    favorite_numbers[i] = favorite_numbers[i + 1];
}

Now 99 is gone, and the array looks like [100, 5, 23, 7, ?].

Summary

Operation Meaning Example
Create     Declare and assign values     int nums[5] = {1,2,3,4,5};
Insert     Place a value in an index     nums[2] = 10;
Delete     Shift elements to remove one     loop shifting technique
Display     Print array elements     for loop or printf


No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...