Paramclasses Academy - Blog

Tutorials Details

←
Post 10 of 15
→

What is Arrays in C programming

What is Arrays in C programming

October 14, 2026

1. Introduction to Arrays

In C programming, an array is a collection of elements of the same data type stored in contiguous memory locations. Arrays allow us to store multiple values under a single variable name instead of declaring separate variables for every value.

For example, instead of writing:

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

We can store all five marks in one array:

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

Arrays make programs easier to organize, process, and maintain.

Learning objectives

By the end of this module, you will be able to:

  • Understand arrays and array indexing.

  • Declare, initialize, and access arrays.

  • Use loops to process array elements.

  • Perform common operations such as sum, average, maximum, and minimum.

  • Work with two-dimensional arrays and matrices.

  • Understand common array errors.

2. What Is an Array?

An array stores multiple values of the same data type in a sequence of memory locations.

Example:

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

Conceptual representation:

Array: numbers

10

Index 0

20

Index 1

30

Index 2

40

Index 3

50

Index 4

Array indexing starts at 0, not 1.

The first element is at index 0, the second at index 1, and the last element of a five-element array is at index 4.

3. Declaring an Array

Syntax

data_type array_name[size];

Example:

int numbers[5];
float prices[10];
char letters[4];

Explanation:

  • int specifies the type of elements.

  • numbers is the array name.

  • 5 specifies the number of elements the array can hold.

In a normal local array declaration, the size must be a valid positive integer expression allowed by C. A variable-length array may be supported in some C versions and implementations.

4. Initializing an Array

Array initialization assigns values when the array is declared.

Example 1: Complete initialization

#include <stdio.h>

int main(void) {
    int numbers[5] = {10, 20, 30, 40, 50};

    printf("%d\n", numbers[0]);
    printf("%d\n", numbers[2]);
    printf("%d\n", numbers[4]);

    return 0;
}

Output:

10
30
50

Example 2: Let the compiler determine the size

int numbers[] = {5, 10, 15, 20};

The compiler determines that the array contains four elements.

Example 3: Partial initialization

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

The remaining elements are initialized to zero:

10 20 0 0 0

Important: An uninitialized local array does not automatically contain zeros. Its elements have indeterminate values until initialized or assigned.

5. Accessing and Modifying Array Elements

Use the index inside square brackets to access or modify an element.

#include <stdio.h>

int main(void) {
    int numbers[3] = {10, 20, 30};

    printf("Original value: %d\n", numbers[1]);

    numbers[1] = 99;

    printf("Updated value: %d\n", numbers[1]);

    return 0;
}

Output:

Original value: 20
Updated value: 99

Here, numbers[1] refers to the second element.

Important: Accessing an index outside the valid range causes undefined behavior. For an array of size 3, valid indexes are 0, 1, and 2.

6. Traversing an Array Using a for Loop

Array traversal means visiting each element one by one.

#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]);
    }

    printf("\n");
    return 0;
}

Output:

10 20 30 40 50

Explanation:

  • i = 0 starts at the first element.

  • i < 5 keeps the loop within the array.

  • numbers[i] accesses the current element.

  • i++ advances to the next index.

A loop is generally more convenient than writing a separate statement for every array element.

7. Taking Array Input from the User

#include <stdio.h>

int main(void) {
    int numbers[5];

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

    for (int i = 0; i < 5; i++) {
        if (scanf("%d", &numbers[i]) != 1) {
            printf("Invalid input.\n");
            return 1;
        }
    }

    printf("You entered: ");

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

    printf("\n");
    return 0;
}

Sample input:

10 20 30 40 50

Output:

You entered: 10 20 30 40 50

The expression &numbers[i] provides the address where scanf() should store the input.

8. Common Array Operations

A. Calculate the sum and average

#include <stdio.h>

int main(void) {
    int marks[5] = {80, 75, 90, 85, 70};
    int sum = 0;

    for (int i = 0; i < 5; i++) {
        sum += marks[i];
    }

    double average = sum / 5.0;

    printf("Total marks: %d\n", sum);
    printf("Average marks: %.2f\n", average);

    return 0;
}

Output:

Total marks: 400
Average marks: 80.00

The expression 5.0 ensures floating-point division, allowing the average to contain a fractional part.

B. Find the largest element

#include <stdio.h>

int main(void) {
    int numbers[5] = {12, 45, 7, 89, 34};
    int maximum = numbers[0];

    for (int i = 1; i < 5; i++) {
        if (numbers[i] > maximum) {
            maximum = numbers[i];
        }
    }

    printf("Largest element: %d\n", maximum);

    return 0;
}

Output:

Largest element: 89

The algorithm begins with the first element as the current maximum, then updates it whenever a larger value is found.

C. Find the smallest element

#include <stdio.h>

int main(void) {
    int numbers[5] = {12, 45, 7, 89, 34};
    int minimum = numbers[0];

    for (int i = 1; i < 5; i++) {
        if (numbers[i] < minimum) {
            minimum = numbers[i];
        }
    }

    printf("Smallest element: %d\n", minimum);

    return 0;
}

Output:

Smallest element: 7

D. Search for an element

Linear search checks elements one by one until it finds the target or reaches the end of the array.

#include <stdio.h>

int main(void) {
    int numbers[5] = {10, 25, 30, 45, 50};
    int target = 30;
    int found_index = -1;

    for (int i = 0; i < 5; i++) {
        if (numbers[i] == target) {
            found_index = i;
            break;
        }
    }

    if (found_index != -1) {
        printf("Element found at index %d\n", found_index);
    } else {
        printf("Element not found\n");
    }

    return 0;
}

Output:

Element found at index 2

The value -1 is used as a marker to indicate that no matching index was found.

E. Reverse an array

#include <stdio.h>

int main(void) {
    int numbers[5] = {1, 2, 3, 4, 5};

    for (int i = 0, j = 4; i < j; i++, j--) {
        int temp = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp;
    }

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

    printf("\n");
    return 0;
}

Output:

5 4 3 2 1

The program swaps the first and last elements, then the second and second-last elements, until the indexes meet.

9. Two-Dimensional Arrays

A two-dimensional array stores data in rows and columns. It is useful for representing matrices, tables, and grids.

Syntax

data_type array_name[rows][columns];

Example:

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

Conceptual representation:

Matrix: matrix[2][3]

1

[0][0]

2

[0][1]

3

[0][2]

4

[1][0]

5

[1][1]

6

[1][2]

The first index represents the row; the second represents the column.

Accessing a 2D array

printf("%d\n", matrix[0][0]);
printf("%d\n", matrix[1][2]);

Output:

1
6

Displaying a 2D array using nested loops

#include <stdio.h>

int main(void) {
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

1 2 3
4 5 6

The outer loop processes rows, while the inner loop processes columns.

10. Practical Example: Student Marks Management

This program accepts marks for five students, calculates the average, and identifies the highest mark.

#include <stdio.h>

int main(void) {
    int marks[5];
    int total = 0;
    int highest;

    for (int i = 0; i < 5; i++) {
        printf("Enter marks for student %d: ", i + 1);

        if (scanf("%d", &marks[i]) != 1) {
            printf("Invalid input.\n");
            return 1;
        }

        total += marks[i];
    }

    highest = marks[0];

    for (int i = 1; i < 5; i++) {
        if (marks[i] > highest) {
            highest = marks[i];
        }
    }

    printf("Total marks: %d\n", total);
    printf("Average marks: %.2f\n", total / 5.0);
    printf("Highest marks: %d\n", highest);

    return 0;
}

Sample input:

80
75
90
85
70

Output:

Total marks: 400
Average marks: 80.00
Highest marks: 90

This example combines array input, traversal, accumulation, and maximum-value searching in one program.

11. Common Array Mistakes

Mistake

Explanation

Starting at index 1

The first element is at index 0.

Using i <= size

This accesses one element beyond the valid range. Use i < size.

Reading uninitialized elements

Initialize elements or assign values before reading them.

Mixing data types

A standard array stores elements of one declared type.

Forgetting input validation

Check whether scanf() successfully reads each value.

Incorrect 2D indexes

Check both the row and column ranges.

Assuming arrays know their own size

A function receiving an array parameter generally needs the size passed separately.

Important: C does not automatically perform bounds checking on ordinary array access.

12. Practical Exercises

Try solving these problems without looking at the solutions first.

Exercise checklist

0 of 7 completed

 

1. Print all elements

Create an integer array containing 10 values and print each element using a loop.

 

2. Calculate the sum

Read 10 integers from the user and calculate their sum.

 

3. Find maximum and minimum

Find the largest and smallest elements in an array.

 

4. Count even and odd numbers

Count how many elements are even and how many are odd.

 

5. Linear search

Search for a user-provided value and display its index if found.

 

6. Reverse an array

Reverse the order of the elements without creating a second array.

 

7. Matrix addition

Add two 2×2 matrices and display the resulting matrix.

13. Interview Questions and Answers

Q1. What is an array in C?

An array is a collection of elements of the same data type stored in contiguous memory locations.

Q2. What is the first index of an array?

The first index is 0.

Q3. How do you declare an integer array of size 10?

int numbers[10];

Q4. What is the difference between a 1D and a 2D array?

A one-dimensional array uses one index, while a two-dimensional array uses two indexes to represent rows and columns.

Q5. Can an array store different data types in the same ordinary array?

No. All elements of an ordinary array have the same declared element type.

Q6. What happens if an array is accessed outside its valid range?

The behavior is undefined in C.

Q7. How do you find the number of elements in an array declared in the same scope?

For a complete array object, you can use:

sizeof(numbers) / sizeof(numbers[0])

This works where numbers is still an actual array, not a function parameter that has been adjusted to a pointer.

Q8. What is linear search?

Linear search checks each element sequentially until the target is found or all elements have been checked.

14. Quick Revision

  • An array stores multiple elements of the same type.

  • Array indexes begin at 0.

  • Array declarations specify an element type, name, and size.

  • Loops are commonly used to access array elements.

  • Common operations include summing, searching, finding maximum and minimum values, and reversing.

  • Two-dimensional arrays represent rows and columns.

  • Always ensure that array indexes remain within valid bounds.

15. Homework

Build a Student Marks Analysis Program with the following requirements:

  1. Read marks for 10 students.

  2. Display all marks.

  3. Calculate the total and average.

  4. Find the highest and lowest marks.

  5. Count how many students passed and failed using a passing threshold of your choice.

  6. Search for a particular mark.

  7. Display the results with clear labels.

Validate input and decide how your program should handle marks outside the valid range of 0 to 100.

Conclusion

Arrays are a fundamental C programming concept. They make it possible to work efficiently with collections of data and provide the foundation for more advanced topics such as strings, multidimensional data, sorting, searching, and pointers.

←
Post 10 of 15
→