Lesson 39 of 60 – Arrays in C
65%

Arrays in C

An array is a collection of elements of the same data type stored together. Arrays are useful when we need to store multiple values using a single variable name.

Note: C array indexing starts from 0. For example, the first element of an array is at index 0, the second at index 1, and so on.

1. What is an Array?

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

int marks[5];

The array marks can store five integer values.

2. Why Use Arrays?

Suppose we want to store the marks of five students. Without an array, we could create five separate variables.

int mark1;
int mark2;
int mark3;
int mark4;
int mark5;

An array makes this easier:

int marks[5];

3. Array Declaration

The basic syntax for declaring an array is:

data_type array_name[size];

Example:

int numbers[5];
  • int is the data type.
  • numbers is the array name.
  • 5 is the number of elements.

4. Declaring an Integer Array

int numbers[5];

This creates an array capable of storing five integer elements.

The indexes are:

0  1  2  3  4

5. Array Index

Each element of an array has an index. The first index is always 0.

int numbers[5];

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

The last index of an array with five elements is 4.

6. Initializing an Array

An array can be initialized when it is declared.

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

The values are assigned to the array elements from left to right.

7. Array Initialization Without Size

When an array is initialized with values, its size can be omitted.

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

The compiler determines the number of elements from the initializer. Here, the array contains four elements.

8. Accessing an Array Element

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

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

Output:

10

The expression numbers[0] accesses the first element.

9. Accessing Different Elements

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

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

Output:

10
20
30
40

10. Changing an Array Element

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

numbers[1] = 100;

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

Output:

100

Array elements can be changed using their indexes.

11. Traversing an Array

Traversing means visiting each element of an array, usually with a loop.

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

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

The loop accesses every element from index 0 to 4.

12. Array with for Loop

#include <stdio.h>

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

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

    return 0;
}

13. Taking Array Input

A loop can be used to take input for each array element.

int numbers[5];

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

Each input value is stored at the corresponding index.

14. Complete Array Input Program

#include <stdio.h>

int main()
{
    int numbers[5];

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

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

    printf("Array elements:\n");

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

    return 0;
}

15. Sum of Array Elements

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

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

printf("Sum = %d", sum);

Output:

Sum = 150

16. Finding the Largest Element

int numbers[] = {25, 10, 45, 30, 20};
int largest = numbers[0];

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

printf("Largest = %d", largest);

The first element is used as the initial largest value.

17. Finding the Smallest Element

int numbers[] = {25, 10, 45, 30, 20};
int smallest = numbers[0];

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

printf("Smallest = %d", smallest);

18. Counting Even Numbers

int numbers[] = {10, 15, 20, 25, 30};
int count = 0;

for (int i = 0; i < 5; i++)
{
    if (numbers[i] % 2 == 0)
    {
        count++;
    }
}

printf("Even numbers = %d", count);

The modulo operator is used to check whether each number is even.

19. Average of Array Elements

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

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

float average = sum / 5.0f;

printf("Average = %.2f", average);

Using 5.0f makes the division floating-point.

20. Array of Floating-Point Values

Arrays can store other data types such as float.

float prices[4] = {
    10.5f,
    20.5f,
    30.5f,
    40.5f
};

for (int i = 0; i < 4; i++)
{
    printf("%.2f\n", prices[i]);
}

21. Character Array

A character array stores characters.

char letters[5] = {'A', 'B', 'C', 'D', 'E'};

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

Character arrays are also used to represent strings when a terminating null character is included.

22. Array Size with sizeof

For an array declared in the same scope, sizeof can be used to calculate the total number of bytes occupied by the array.

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

int size = sizeof(numbers) / sizeof(numbers[0]);

printf("Elements = %d", size);

The result is 5.

23. Passing an Array to a Function

An array can be passed to a function for processing.

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

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

    display(numbers, 3);

    return 0;
}

The size is commonly passed separately.

24. Modifying an Array in a Function

void change(int numbers[])
{
    numbers[0] = 100;
}

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

    change(numbers);

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

    return 0;
}

Output:

100

When an array is passed to a function, the function can modify its elements through the parameter.

25. Out-of-Bounds Array Access

Never access an index outside the valid range of an array.

int numbers[5];

numbers[0] = 10;
numbers[4] = 50;

/* Invalid access */
numbers[5] = 60;

For an array of five elements, valid indexes are only 0 through 4. C does not perform automatic bounds checking.

26. Array Initialization with Fewer Values

If an array is initialized with fewer values than its specified size, the remaining elements are initialized to zero.

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

The elements are effectively:

10 20 0 0 0

27. Complete Array Sum Program

#include <stdio.h>

int main()
{
    int numbers[5];
    int sum = 0;

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

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

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

    printf("Sum = %d", sum);

    return 0;
}

28. Complete Largest Element Program

#include <stdio.h>

int main()
{
    int numbers[5];

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

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

    int largest = numbers[0];

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

    printf("Largest = %d", largest);

    return 0;
}

29. Common Array Mistakes

  • Forgetting that array indexes start from 0.
  • Accessing an index outside the array's valid range.
  • Using the wrong array size in a loop.
  • Forgetting to initialize an array when necessary.
  • Using an uninitialized local array and assuming its values are zero.
  • Forgetting to pass the array size when a function needs it.
int numbers[5];

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

For a local array like this, the elements are not automatically initialized to zero.

30. Practice Programs on Arrays

Practice the following programs:

  1. Store and display five numbers using an array.
  2. Take five numbers from the user and display them.
  3. Find the sum of array elements.
  4. Find the average of array elements.
  5. Find the largest element.
  6. Find the smallest element.
  7. Count even numbers in an array.
  8. Count odd numbers in an array.
  9. Search for a specific value in an array.
  10. Pass an array to a function and display its elements.

Practice using loops with arrays because loops are commonly used to traverse array elements.

📌 Key Points

  • An array stores multiple values of the same data type.
  • Array indexing starts from 0.
  • The last index of an array of size n is n - 1.
  • Arrays can be initialized during declaration.
  • A loop can be used to traverse an array.
  • Array elements can be read and modified using indexes.
  • sizeof can help calculate the number of elements of an array in its original scope.
  • Arrays can be passed to functions.
  • The size of an array is commonly passed separately to a function.
  • Accessing an array outside its valid range is an error in program logic and can cause undefined behavior.

🧠 Quick Quiz

Question: What is the index of the first element of a C array?