Skip to main content
C Programming Basics
CHAPTER 10 Beginner

Arrays in C

Updated: May 17, 2026
5 min read

# CHAPTER 10

Arrays in C

1. Introduction

Imagine you need to store the grades of 100 students. Creating 100 individual variables (grade1, grade2, ..., grade100) is highly inefficient. An Array is a data structure that stores a fixed-size sequential collection of elements of the *same type*.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Declare and initialize arrays.
  • Access array elements using indices.
  • Traverse arrays using loops.
  • Understand how arrays are stored in memory.
  • Perform basic searching in an array.

3. Declaring and Initializing Arrays

Declaration Syntax: dataType arrayName[arraySize];
c
1
int grades[5]; // Declares an array of 5 integers

Initialization:

c
12345678
// Method 1: Initializing at declaration
int scores[5] = {85, 90, 78, 92, 88};

// Method 2: Size is inferred from the elements
int ages[] = {20, 22, 21, 25}; 

// Method 3: Partial initialization (rest become 0)
int zeros[5] = {10, 20}; // {10, 20, 0, 0, 0}

4. Accessing Array Elements

Arrays are Zero-Indexed. The first element is at index 0.
c
1234567
int scores[5] = {85, 90, 78, 92, 88};

printf("First score: %d\n", scores[0]); // 85
printf("Third score: %d\n", scores[2]); // 78

// Modifying an element
scores[1] = 95; // Changes the second element from 90 to 95

5. Traversing an Array with Loops

To print or process all elements, we use a for loop.
c
12345
int scores[5] = {85, 95, 78, 92, 88};

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

6. Passing Arrays to Functions

When you pass an array to a function, you are actually passing its *memory address* (which we will cover fully in pointers). Because of this, you must also pass the size of the array.
c
1234567891011121314
#include <stdio.h>

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

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

7. Mini Project: Marks Management System

Let's calculate the total and average marks of a student.
c
1234567891011121314151617181920212223242526
#include <stdio.h>

int main() {
    int marks[5];
    int sum = 0;
    float average;

    printf("=== MARKS MANAGEMENT ===\n");
    // Get input
    for (int i = 0; i < 5; i++) {
        printf("Enter marks for subject %d: ", i + 1);
        scanf("%d", &marks[i]);
    }

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

    average = (float)sum / 5; // Type casting for decimal accuracy

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

    return 0;
}

8. Memory-Level Explanation

Arrays are stored in contiguous (continuous) memory locations. If an int takes 4 bytes, an array of 3 ints takes 12 contiguous bytes.
12345678910
int arr[3] = {10, 20, 30};

Memory Layout:
+----------------+
| arr[0] = 10    | <- Address 2000
+----------------+
| arr[1] = 20    | <- Address 2004
+----------------+
| arr[2] = 30    | <- Address 2008
+----------------+

Because the memory is contiguous, the CPU can access elements instantly using math: Address = Base_Address + (Index * Data_Size).

9. Common Mistakes

  • Out of Bounds Access: In C, there is no boundary checking. If you declare int arr[5], and try to access arr[10], C will blindly read whatever is in that memory location, causing garbage values or a Segmentation Fault (crash).
  • Index starts at 1: Forgetting that arrays start at 0. The last element of arr[5] is arr[4].

10. Exercises

  1. 1. Write a program to find the maximum number in an array.
  1. 2. Write a program that reverses the elements of an array.
  1. 3. Write a program to search for a specific number in an array (Linear Search) and print its index.

11. MCQ Quiz with Answers

Question 1

What is the index of the first element in an array?

Question 2

How is memory allocated for an array?

Question 3

If int arr[5] = {1, 2};, what is the value of arr[3]?

Question 4

What happens if you access arr[10] on an array of size 5?

Question 5

How do you pass an array to a function?

Question 6

What does sizeof(arr) return if int arr[5]; (assuming 4-byte int)?

Q7. Can an array store mixed data types (e.g., int and float)? a) Yes b) No Answer: b) No
Question 8

Which loop is most commonly used to traverse an array?

Question 9

If an array starts at memory address 1000, and stores 4-byte integers, what is the address of arr[2]?

Question 10

How do you find the number of elements in an array dynamically?

12. Interview Questions

  • Q: Why do arrays in C start at index 0?
  • Q: How is an array passed to a function (pass by value or reference)? Explain why.
  • Q: What is the formula to calculate the memory address of the i-th element of a 1D array?

13. Summary

Arrays store multiple elements of the same type in contiguous memory. They are zero-indexed, meaning traversal loops usually go from 0 to size - 1. Arrays are efficient for accessing data but lack built-in bounds checking, requiring the programmer to manage bounds carefully.

14. Next Chapter Recommendation

In Chapter 11: Strings in C, we will look at a special type of array: character arrays, and how C handles text data.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·