Arrays in C

1️⃣ What Is an Array?

An array is a collection of variables of the same type, stored contiguously in memory.

Instead of creating individual variables like:

int a = 10;
int b = 20;
int c = 30;

You can use an array:

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

✔️ Arrays make it easy to handle multiple values with a single name.


2️⃣ Why Use Arrays?

  • ✔️ Store many items of the same type efficiently.
  • ✔️ Access items easily using an index.
  • ✔️ Useful when the number of items is fixed or known in advance.

3️⃣ Declaring an Array

Syntax:

type arrayName[size];

Example:

int numbers[5];      // Array of 5 integers
float prices[10];    // Array of 10 floats
char letters[26];    // Array of 26 characters

4️⃣ Initializing an Array

🛠 Full Initialization:

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

✅ Partial Initialization:

int numbers[5] = {1, 2};  // Remaining elements are set to 0

🔥 Shorthand (if size is auto-detected):

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

5️⃣ Accessing Array Elements

  • Indexing starts at 0.
  • Use square brackets [] to access elements.

Example:

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

6️⃣ Modifying Array Elements

numbers[2] = 10;  // Change the 3rd element to 10

🛠 Full Example: Working with Arrays

#include <stdio.h>

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

    // Print all elements
    for (int i = 0; i < 5; i++) {
        printf("Element %d: %d\n", i, numbers[i]);
    }

    // Update an element
    numbers[2] = 99;
    printf("Updated Element 2: %d\n", numbers[2]);

    return 0;
}

7️⃣ Input and Output with Arrays

Example: Read and Print 5 Numbers

#include <stdio.h>

int main() {
    int numbers[5];

    // Input
    printf("Enter 5 numbers:\n");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &numbers[i]);
    }

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

    return 0;
}

8️⃣ Multi-Dimensional Arrays

A 2D array is like a table (matrix) with rows and columns.

Syntax:

type arrayName[rows][columns];

Example:

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

Accessing an element:

printf("%d\n", matrix[1][2]);  // Prints 6

🛠 2D Array Example

#include <stdio.h>

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

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

    return 0;
}

🔧 Array Limitations

  • ❌ Fixed size (must know size at compile time).
  • ❌ No bounds checking (C won’t stop you if you access out-of-bounds memory).
  • ❌ All elements must be of the same type.

✅ When to Use Arrays

Use CaseWhy Arrays?
Store a list of itemsNeed to handle multiple similar values (e.g., marks, scores).
Iterate over dataArrays make looping through items simple and fast.
Matrices & tables2D arrays are great for grid-like data (like a chessboard).
Static-sized data collectionsBest when the size is fixed and known ahead of time.

🔚 Conclusion

  • Arrays store multiple items of the same type in one block of memory.
  • You access elements using an index, starting at 0.
  • Arrays can be 1D, 2D, or multi-dimensional.
  • Be careful: no bounds checking in C!

💡 Next challenge: Try writing a program to find the largest element in an array.

Feedback Display

Learner Reviews