Arrays in C Programming: Complete Guide from Beginners to Advanced with Examples & Practice Programs

Arrays in C Programming allow you to store and manage multiple values under a single name, making data handling easier and more organized. They are useful for reducing repetitive code, performing calculations efficiently, and processing large amounts of data.

In real life, Arrays are commonly used to store student marks, employee salaries, product prices, customer data, sales records, and other large datasets. In this chapter, you will learn Arrays from Beginner to Advanced level with practical examples and practice programs.

🧑‍💻 Chapter 1: C Programming Fundamentals
📜 Chapter 2: History and Evolution of C Language
⚙️ Chapter 3: Installing a C Compiler
💻 Chapter 4: Writing Your First C Program
🏗️ Chapter 5: Structure of a C Program
📦 Chapter 6: Variables and Data Types
🔢 Chapter 7: Operators in C
⌨️ Chapter 8: Input and Output Functions
🔀 Chapter 9: Decision Making and Branching – Conditional Statements
🔄 Chapter 10: Decision Making and Loops in C Programming
📊 Chapter 11: Arrays – Current Chapter
🔤 Chapter 12: Strings
🛠️ Chapter 13: Functions
👉 Chapter 14: Pointers
🏛️ Chapter 15: Structures and Unions
📁 Chapter 16: File Handling

Arrays are extremely useful when working with student marks, product prices, employee salaries, customer records, sales data, scores, and large datasets. They also form an important foundation for learning strings, matrices, searching, sorting, data structures, and algorithms.

An array is a collection of multiple values of the same data type stored under one variable name.

For example, suppose you want to store the marks of five students.

Without an array, you might write:

int mark1 = 85;
int mark2 = 78;
int mark3 = 92;
int mark4 = 88;
int mark5 = 76;

This works, but it becomes difficult to manage when the number of values becomes large.

With an array, the same data can be stored using one variable:

int marks[5] = {85, 78, 92, 88, 76};

Now all five values are stored in the marks array.

Another Simple Definition:

  • An array is a derived data type in C.
  • An array is a collection of elements of the same data type stored under a single name.
  • It is a homogeneous collection, which means all elements in an array have the same data type.
  • Array elements are stored in contiguous memory locations.
  • Each element can be accessed using its index number.

Example:

int numbers[5] = {10, 20, 30, 40, 50};

Here, numbers is an integer array that can store 5 integer values.

Imagine a school has the marks of 1,000 students.

Creating 1,000 separate variables would be difficult:

int mark1;
int mark2;
int mark3;
...
int mark1000;

Using an array, we can simply write:

int marks[1000];

This makes the program much shorter and easier to manage.

  • Store multiple values using one variable name.
  • Reduce repetitive code.
  • Make data easier to organize.
  • Make it easier to process large amounts of data.
  • Work efficiently with loops.
  • Useful for searching and sorting.
  • Help in creating matrices and tables.
  • Provide the foundation for many advanced data structures.

Arrays are not limited to textbook programs. They are used in many real-world applications.

Arrays can be used to store:

  • Product prices
  • Product IDs
  • Stock quantities
  • Product ratings
  • Customer-related data

Arrays can help manage:

  • Account balances
  • Transaction records
  • Customer information
  • Account numbers

Arrays can store:

  • Player scores
  • Game levels
  • High scores
  • Positions of game objects

Arrays can be used for:

  • Test results
  • Numerical patient data
  • Measurements
  • Medical datasets

Large collections of numerical data can be stored and processed using arrays.

In short:

Real-World Data
      ↓
   Array
      ↓
Store → Process → Search → Sort → Analyze

Suppose we create:

int marks[5] = {87, 78, 91, 80, 76};

The array contains five elements.

An array uses an index to identify each element.

In C, array indexing starts from 0, not 1.

Array:     marks

Index:       0     1     2     3     4
             ↓     ↓     ↓     ↓     ↓
Value:      87    78    91    80    76

Therefore:

marks[0] = 87
marks[1] = 78
marks[2] = 91
marks[3] = 80
marks[4] = 76

Important Point for YOU:

For an array of size 5:

int marks[5];

valid indexes are:

0  1  2  3  4

The last index is:

size - 1

So:

5 - 1 = 4

Indexing is one of the most important concepts in arrays.

Consider:

int numbers[5] = {10, 20, 30, 40, 50};

You can access individual values using:

printf("%d", numbers[0]);

Output:

10

Similarly:

printf("%d", numbers[3]);

Output:

40

Always Remember:

First element → index 0
Second element → index 1
Third element → index 2
...
Last element → index size - 1

The basic syntax for declaring an array is:

data_type array_name[size];

Example

int marks[5];

Here:

  • int → data type
  • marks → array name
  • 5 → number of elements

Another example:

float prices[10];

This creates an array capable of storing 10 float values.

We can assign values to an array when declaring it.

Example

int numbers[5] = {10, 20, 30, 40, 50};

The values are stored as:

Index       Value

  0          10
  1          20
  2          30
  3          40
  4          50

We can also allow the compiler to determine the size:

int numbers[] = {10, 20, 30, 40, 50};

Here, the compiler determines that the array contains five elements.

Array elements are accessed using:

array_name[index]

Example:

int numbers[5] = {10, 20, 30, 40, 50};

printf("%d", numbers[2]);

Output:

30

Because:

numbers[2] → 30

Array elements can also be modified.

int numbers[5] = {10, 20, 30, 40, 50};

numbers[2] = 100;

Now the array becomes:

10  20  100  40  50

Traversal means visiting each element of an array one by one.

A for loop is commonly used for this.

#include <stdio.h>

int main(void) {

    int numbers[5] = {10, 20, 30, 40, 50};

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

    return 0;
}

Output

10 20 30 40 50

How It Works

The loop starts with:

i = 0

Then:

numbers[0] → 10
numbers[1] → 20
numbers[2] → 30
numbers[3] → 40
numbers[4] → 50

This is why arrays and loops are closely connected.

#include <stdio.h>

int main(void) {

    int arr[5] = {10, 20, 30, 40, 50};

    printf("Array elements are:\n");

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

    return 0;
}

Output

Array elements are:
10 20 30 40 50

Arrays can be classified according to their dimensions.

The commonly used types are:

  1. One-Dimensional Array
  2. Two-Dimensional Array
  3. Multidimensional Array

A one-dimensional array stores elements in a single sequence or row.

Example

int numbers[5] = {10, 20, 30, 40, 50};

Visual representation:

        One-Dimensional Array

Index     0     1     2     3     4
          ↓     ↓     ↓     ↓     ↓
Value    10    20    30    40    50

Real-Life Example

Student marks can be stored as:

int marks[5] = {85, 78, 92, 88, 76};

Question: Write a program to enter and display the marks of a student in 3 subjects and also display the total and average marks.

#include <stdio.h>

int main(void)
{
    int M[3], i, total = 0;
    float avg;

    printf("INPUT DETAILS\n");

    for (i = 0; i < 3; i++)
    {
        printf("Enter marks in subject %d: ", i + 1);
        scanf("%d", &M[i]);
    }

    printf("\nOUTPUT DETAILS\n");

    for (i = 0; i < 3; i++)
    {
        printf("Subject [%d] = %d\n", i + 1, M[i]);
        total = total + M[i];
    }

    avg = total / 3.0f;

    printf("Total = %d\n", total);
    printf("Average = %.2f\n", avg);

    return 0;
}

Example Output:

INPUT DETAILS
Enter marks in subject 1: 80
Enter marks in subject 2: 75
Enter marks in subject 3: 90

OUTPUT DETAILS
Subject [1] = 80
Subject [2] = 75
Subject [3] = 90
Total = 245
Average = 81.67

Question: Write a program to find the largest element from the given array list.

#include <stdio.h>

int main(void)
{
    int A[20], N, i, L;

    printf("Enter the array length: ");
    scanf("%d", &N);

    printf("Enter the array elements:\n");

    for (i = 0; i < N; i++)
    {
        scanf("%d", &A[i]);
    }

    L = A[0];

    for (i = 1; i < N; i++)
    {
        if (A[i] > L)
        {
            L = A[i];
        }
    }

    printf("Largest element = %d\n", L);

    return 0;
}

Example Output:

Enter the array length: 5
Enter the array elements:
10 25 7 45 30

Largest element = 45

Explanation:
The first element is initially considered the largest. The program then compares it with each remaining element. If a larger element is found, it is stored in L.

Question: Write a program to reverse the given array list using a third variable.

#include <stdio.h>

int main(void)
{
    int A[20], N, i, j, temp;

    printf("Enter the array length: ");
    scanf("%d", &N);

    printf("Enter the array elements:\n");

    for (i = 0; i < N; i++)
    {
        scanf("%d", &A[i]);
    }

    for (i = 0, j = N - 1; i < j; i++, j--)
    {
        temp = A[i];
        A[i] = A[j];
        A[j] = temp;
    }

    printf("Reversed list:\n");

    for (i = 0; i < N; i++)
    {
        printf("%5d", A[i]);
    }

    return 0;
}

Example Output:

Enter the array length: 5
Enter the array elements:
10 20 30 40 50

Reversed list:
   50   40   30   20   10

Explanation:
The program uses a third variable temp to swap the first and last elements, then the second and second-last elements, and so on until the array is reversed.

A two-dimensional array stores data in rows and columns.

It is commonly used to represent:

  • Tables
  • Matrices
  • Grids
  • Marksheets
  • Game boards

Example

int matrix[2][3] = {
    {10, 20, 30},
    {40, 50, 60}
};

It can be visualized as:

          Column
           0    1    2
        ┌────┬────┬────┐
Row 0   │ 10 │ 20 │ 30 │
        ├────┼────┼────┤
Row 1   │ 40 │ 50 │ 60 │
        └────┴────┴────┘

To access 50:

matrix[1][1]

Because:

row = 1
column = 1

These programs are examples of Two-Dimensional Arrays (2D Arrays) and are useful for understanding matrices, rows, columns, diagonals, transpose, and matrix operations.

Question: Write a program to print the lower half or lower triangle of a 3×3 matrix.

#include <stdio.h>

int main(void)
{
    int A[3][3], i, j;

    printf("Enter the elements of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nLower triangle of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (j <= i)
                printf("%5d", A[i][j]);
            else
                printf("%5s", "");
        }

        printf("\n");
    }

    return 0;
}

Example

For the following matrix:

1 2 3
4 5 6
7 8 9

The lower triangle is:

1
4 5
7 8 9

Logic

The lower triangle contains elements where:

j <= i

Question: Write a program to print the upper half or upper triangle of a 3×3 matrix.

#include <stdio.h>

int main(void)
{
    int A[3][3], i, j;

    printf("Enter the elements of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nUpper triangle of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (j >= i)
                printf("%5d", A[i][j]);
            else
                printf("%5s", "");
        }

        printf("\n");
    }

    return 0;
}

Example

Input matrix:

1 2 3
4 5 6
7 8 9

Output:

1 2 3
  5 6
    9

Logic

The upper triangle contains elements where:

j >= i

Question: Write a program to print the principal or main diagonal of a 3×3 matrix.

#include <stdio.h>

int main(void)
{
    int A[3][3], i, j;

    printf("Enter the elements of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nPrincipal diagonal of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (i == j)
                printf("%5d", A[i][j]);
            else
                printf("%5s", "");
        }

        printf("\n");
    }

    return 0;
}

Example

Input:

1 2 3
4 5 6
7 8 9

Principal diagonal elements:

1
5
9

Logic

For the principal diagonal:

i == j

The row index and column index are the same.

Question: Write a program to print the secondary or alternate diagonal of a 3×3 matrix.

#include <stdio.h>

int main(void)
{
    int A[3][3], i, j;

    printf("Enter the elements of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nSecondary diagonal of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            if (i + j == 2)
                printf("%5d", A[i][j]);
            else
                printf("%5s", "");
        }

        printf("\n");
    }

    return 0;
}

Example

Input:

1 2 3
4 5 6
7 8 9

Secondary diagonal elements:

3
5
7

Logic

For a 3×3 matrix, the secondary diagonal follows:

i + j == 2

Because:

0 + 2 = 2
1 + 1 = 2
2 + 0 = 2

Question: Write a program to print the transpose of a 3×4 matrix.

#include <stdio.h>

int main(void)
{
    int A[3][4], i, j;

    printf("Enter the elements of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 4; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nTranspose of the matrix:\n");

    for (j = 0; j < 4; j++)
    {
        for (i = 0; i < 3; i++)
        {
            printf("%5d", A[i][j]);
        }

        printf("\n");
    }

    return 0;
}

Example

Original 3×4 matrix:

1  2  3  4
5  6  7  8
9 10 11 12

Transpose:

1  5  9
2  6 10
3  7 11
4  8 12

Important Point

A 3×4 matrix becomes a 4×3 matrix after transposition.

Question: Write a program to print a 3×4 matrix along with its row sums and column sums.

#include <stdio.h>

int main(void)
{
    int A[3][4];
    int Rs[3] = {0};
    int Cs[4] = {0};
    int i, j;

    printf("Enter the elements of the matrix:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 4; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    /* Calculate row sums */
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 4; j++)
        {
            Rs[i] = Rs[i] + A[i][j];
        }
    }

    /* Calculate column sums */
    for (j = 0; j < 4; j++)
    {
        for (i = 0; i < 3; i++)
        {
            Cs[j] = Cs[j] + A[i][j];
        }
    }

    printf("\nMatrix with Row Sums:\n");

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 4; j++)
        {
            printf("%5d", A[i][j]);
        }

        printf("   = %d\n", Rs[i]);
    }

    printf("\nColumn Sums:\n");

    for (j = 0; j < 4; j++)
    {
        printf("%5d", Cs[j]);
    }

    printf("\n");

    return 0;
}

Example

Input:

1  2  3  4
5  6  7  8
9 10 11 12

Output:

    1    2    3    4   = 10
    5    6    7    8   = 26
    9   10   11   12   = 42

Column Sums:
   15   18   21   24

Important Formula

Row sum:

Rs[i] = Rs[i] + A[i][j];

Column sum:

Cs[j] = Cs[j] + A[i][j];

Question: Write a program to add two matrices.

#include <stdio.h>

int main(void)
{
    int A[5][5], B[5][5], C[5][5];
    int i, j, m, n, p, q;

    printf("Enter the number of rows and columns of Matrix A: ");
    scanf("%d %d", &m, &n);

    printf("Enter the number of rows and columns of Matrix B: ");
    scanf("%d %d", &p, &q);

    if (m != p || n != q)
    {
        printf("Matrices cannot be added.\n");
        return 0;
    }

    printf("\nEnter the elements of Matrix A:\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nEnter the elements of Matrix B:\n");

    for (i = 0; i < p; i++)
    {
        for (j = 0; j < q; j++)
        {
            scanf("%d", &B[i][j]);
        }
    }

    printf("\nResultant Matrix:\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            C[i][j] = A[i][j] + B[i][j];
            printf("%5d", C[i][j]);
        }

        printf("\n");
    }

    return 0;
}

Important Rule

Two matrices can be added only when they have the same number of rows and columns.

For example:

2×3 + 2×3 = Possible

But:

2×3 + 3×2 = Not Possible

Question: Write a program to multiply two matrices.

#include <stdio.h>

int main(void)
{
    int A[5][5], B[5][5], C[5][5];
    int i, j, k;
    int m, n, p, q;

    printf("Enter the number of rows and columns of Matrix A: ");
    scanf("%d %d", &m, &n);

    printf("Enter the number of rows and columns of Matrix B: ");
    scanf("%d %d", &p, &q);

    if (n != p)
    {
        printf("Matrices cannot be multiplied.\n");
        return 0;
    }

    printf("\nEnter the elements of Matrix A:\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &A[i][j]);
        }
    }

    printf("\nEnter the elements of Matrix B:\n");

    for (i = 0; i < p; i++)
    {
        for (j = 0; j < q; j++)
        {
            scanf("%d", &B[i][j]);
        }
    }

    /* Calculate resultant matrix */
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < q; j++)
        {
            C[i][j] = 0;

            for (k = 0; k < n; k++)
            {
                C[i][j] = C[i][j] + A[i][k] * B[k][j];
            }
        }
    }

    printf("\nResultant Matrix:\n");

    for (i = 0; i < m; i++)
    {
        for (j = 0; j < q; j++)
        {
            printf("%5d", C[i][j]);
        }

        printf("\n");
    }

    return 0;
}

Important Rule

If:

Matrix A = m × n
Matrix B = p × q

then multiplication is possible only when:

n = p

The resultant matrix will have the size:

m × q

Example

If:

A = 2×3
B = 3×2

then:

A × B = 2×2

An array with more than two dimensions is called a multidimensional array.

For example:

int data[2][3][4];

This represents a 3-dimensional array.

Multidimensional arrays are useful for complex datasets, simulations, scientific calculations, and other applications where data has more than two dimensions.

#include <stdio.h>

int main(void)
{
    int A[2][2][2];
    int i, j, k;

    printf("Enter 8 elements:\n");

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
        {
            for (k = 0; k < 2; k++)
            {
                scanf("%d", &A[i][j][k]);
            }
        }
    }

    printf("\nElements of the 3D array:\n");

    for (i = 0; i < 2; i++)
    {
        printf("Block %d:\n", i + 1);

        for (j = 0; j < 2; j++)
        {
            for (k = 0; k < 2; k++)
            {
                printf("%5d", A[i][j][k]);
            }

            printf("\n");
        }

        printf("\n");
    }

    return 0;
}

Example

Input:

1 2 3 4 5 6 7 8

Output:

Elements of the 3D array:

Block 1:
    1    2
    3    4

Block 2:
    5    6
    7    8

Explanation

A 3D array uses three indexes:

A[i][j][k]

Here:

  • i → Block
  • j → Row
  • k → Column
#include <stdio.h>

int main(void)
{
    int A[2][2][2];
    int i, j, k;
    int sum = 0;

    printf("Enter 8 elements:\n");

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
        {
            for (k = 0; k < 2; k++)
            {
                scanf("%d", &A[i][j][k]);
                sum = sum + A[i][j][k];
            }
        }
    }

    printf("\nSum of all elements = %d\n", sum);

    return 0;
}

Example

Input:

1 2 3 4 5 6 7 8

Calculation:

Sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8
    = 36

Output:

Sum of all elements = 36

Note: Multidimensional Arrays

A Multidimensional Array has more than one dimension:

2D Array → A[i][j]
3D Array → A[i][j][k]
4D Array → A[i][j][k][l]

In C, strings are stored using character arrays.

Example:

char name[] = "Mujtaba";

Internally, the string is stored as characters followed by the null character:

M  u  j  t  a  b  a  \0

The \0 marks the end of the string.

Example:

#include <stdio.h>

int main(void) {

    char name[] = "Mujtaba";

    printf("%s", name);

    return 0;
}

Output:

Mujtaba

Strings and character arrays will become especially important when learning Strings in C.

We can use a loop to take multiple values from the user.

#include <stdio.h>

int main(void) {

    int numbers[5];

    printf("Enter 5 numbers:\n");

    for (int i = 0; i < 5; i++) {
        scanf("%d", &numbers[i]);
    }

    printf("Array elements are:\n");

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

    return 0;
}

Input

10
20
30
40
50

Output

Array elements are:
10 20 30 40 50

Searching means finding whether a particular value exists in an array.

One of the simplest methods is Linear Search.

Example

#include <stdio.h>

int main(void) {

    int numbers[5] = {10, 20, 30, 40, 50};
    int search, found = 0;

    printf("Enter the number to search: ");
    scanf("%d", &search);

    for (int i = 0; i < 5; i++) {

        if (numbers[i] == search) {
            found = 1;
            break;
        }
    }

    if (found) {
        printf("Number found.");
    } else {
        printf("Number not found.");
    }

    return 0;
}

Example

Input:

30

Output:

Number found.

Sorting means arranging array elements in a particular order.

For example:

Before:
50 20 40 10 30

After ascending sorting:

10 20 30 40 50

A simple sorting technique for beginners is Bubble Sort.

#include <stdio.h>

int main(void) {

    int numbers[5] = {50, 20, 40, 10, 30};

    for (int i = 0; i < 5 - 1; i++) {

        for (int j = 0; j < 5 - i - 1; j++) {

            if (numbers[j] > numbers[j + 1]) {

                int temp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = temp;
            }
        }
    }

    printf("Sorted array:\n");

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

    return 0;
}

Output

Sorted array:
10 20 30 40 50

Once you understand arrays, several important operations can be performed on them.

Common Array Operations

             ARRAY
               │
      ┌────────┼────────┐
      ↓        ↓        ↓
 Traversal  Searching  Sorting
      │        │        │
      ↓        ↓        ↓
  Visit all  Find a   Arrange
  elements   value    elements

Other operations include:

  • Insertion
  • Deletion
  • Updating
  • Copying
  • Merging
  • Reversing

Example:

Original:
10 20 30 40 50

Reversed:
50 40 30 20 10
#include <stdio.h>

int main(void) {

    int numbers[5] = {10, 20, 30, 40, 50};

    printf("Reversed array:\n");

    for (int i = 4; i >= 0; i--) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Output:

50 40 30 20 10

Keep these points in mind:

1. Array indexing starts from 0

For:

int arr[5];

indexes are:

0 1 2 3 4

2. Array elements have the same data type

For example:

int arr[5];

stores integer values.

3. Array size represents the number of elements

int arr[10];

can store 10 integers.

4. Do not access an invalid index

For:

int arr[5];

this is valid:

arr[4]

but this is invalid:

arr[5]

The valid indexes end at 4.

5. Array indexing is zero-based

This is one of the most important things to remember as a beginner.

Mistake 1: Using an invalid index

Incorrect:

int arr[5];

arr[5] = 100;

The valid indexes are only:

0 to 4

Mistake 2: Forgetting that indexing starts from 0

Incorrect assumption:

First element = arr[1]

Correct:

First element = arr[0]

Mistake 3: Loop condition goes beyond the array

Incorrect:

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

For an array of size 5, use:

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

Arrays and loops are often used together.

Suppose:

int arr[5] = {10, 20, 30, 40, 50};

A loop allows us to process every element:

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

Think of it like this:

i = 0 → arr[0] → 10
i = 1 → arr[1] → 20
i = 2 → arr[2] → 30
i = 3 → arr[3] → 40
i = 4 → arr[4] → 50

This is one of the most important patterns to understand before moving to advanced array programs.

FeatureNormal VariableArray
StoresOne valueMultiple values
Data typeOne typeSame type for elements
Exampleint ageint ages[10]
AccessVariable nameIndex
Useful forSingle dataCollection of related data

Example:

int age = 25;

stores one value.

Whereas:

int ages[5] = {20, 21, 22, 23, 24};

stores five values.

Consider:

int arr[5] = {10, 20, 30, 40, 50};

The array contains:

5 elements

Indexes:

0 1 2 3 4

The relationship is:

Last Index = Number of Elements - 1

Therefore:

5 - 1 = 4

Array elements are stored in contiguous memory locations.

For example:

int arr[4] = {10, 20, 30, 40};

Conceptually:

Memory

┌────────┬────────┬────────┬────────┐
│  arr[0]│  arr[1]│  arr[2]│  arr[3]│
│   10   │   20   │   30   │   40   │
└────────┴────────┴────────┴────────┘
     ↓        ↓        ↓        ↓
  nearby   nearby   nearby   nearby
 addresses in memory

Because array elements are stored consecutively, accessing an element using its index is efficient.

This concept becomes especially important when you learn Pointers in C.

Arrays can also be passed to functions.

Example:

#include <stdio.h>

void printArray(int arr[], int size) {

    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
}

int main(void) {

    int numbers[5] = {10, 20, 30, 40, 50};

    printArray(numbers, 5);

    return 0;
}

Output:

10 20 30 40 50

This becomes useful when programs become larger and you want to divide your code into separate functions.

After learning the basics, practice these programs:

Basic Programs

  1. Declare and initialize an array.
  2. Print all array elements.
  3. Take array input from the user.
  4. Find the sum of array elements.
  5. Find the average of array elements.
  6. Find the largest element.
  7. Find the smallest element.
  8. Count even and odd elements.
  9. Count positive and negative elements.
  10. Reverse an array.

Searching Programs

  1. Linear search.
  2. Find the position of an element.
  3. Count occurrences of an element.
  4. Find duplicate elements.

Sorting Programs

  1. Sort an array in ascending order.
  2. Sort an array in descending order.
  3. Implement Bubble Sort.
  4. Implement Selection Sort.

2D Array Programs

  1. Print a matrix.
  2. Add two matrices.
  3. Subtract two matrices.
  4. Multiply two matrices.
  5. Find the transpose of a matrix.
  6. Find the sum of matrix elements.
  7. Find diagonal elements.

Advanced Practice

  1. Pass an array to a function.
  2. Search an array using a function.
  3. Sort an array using a function.
  4. Work with character arrays.
  5. Practice multidimensional arrays.

What is an Array?

An array stores multiple values of the same data type under one variable name.

Array Indexing

Starts from 0

One-Dimensional Array

int arr[5];

Two-Dimensional Array

int matrix[2][3];

Access an Element

arr[index]

Traverse an Array

for (int i = 0; i < size; i++) {
    printf("%d ", arr[i]);
}

Important Operations

Traversal
Searching
Sorting
Insertion
Deletion
Updating
Reversing

NOTE:

Array = Multiple related values
Index = Position of an element
Loop = Process elements repeatedly

What is an array in C?

An array is a collection of elements of the same data type stored under a single variable name.

Why are arrays used in C?

Arrays are used to store and process multiple related values efficiently.

Does array indexing start from 0 in C?

Yes. The first element is stored at index 0.

What is a one-dimensional array?

A one-dimensional array stores elements in a single sequence.

Example:

int numbers[5];

What is a two-dimensional array?

A two-dimensional array stores data in rows and columns.

Example:

int matrix[3][3];

Can an array store different data types?

No. The elements of a normal C array have the same data type.

For example:

int arr[5];

stores integers.

How do you access an array element?

Use the array name with its index:

arr[0]

What is array traversal?

Array traversal means visiting or processing each array element one by one, usually with a loop.

What happens if we access an invalid array index?

Accessing an element outside the valid range causes undefined behavior in C. It can produce incorrect results or other unexpected behavior.

What is the last index of an array of size 10?

The last valid index is:

9

because:

10 - 1 = 9

Can arrays be passed to functions?

Yes. Arrays can be passed to functions, which is very useful for creating modular C programs.



Leave a Comment

Your email address will not be published. Required fields are marked *