Introduction to Arrays in C

In C programming, arrays are one of the most fundamental concepts for storing and processing data. An array is a collection of elements of the same type stored in contiguous memory locations. Instead of declaring multiple variables to store similar values, an array lets us store and manage them efficiently. For example, instead of creating five separate integer variables (int a, b, c, d, e;), we can declare a single array (int numbers[5];) that stores all values in one place.

Arrays are especially useful when dealing with large amounts of data, such as marks of students, sales figures of products, or sensor readings in scientific experiments. They also serve as the foundation for more advanced data structures like matrices, strings, stacks, and queues.

In this lecture, we will explore arrays in C programming with simple examples, step-by-step explanations, outputs, and practice problems. By the end of this lesson, you will be able to create and use arrays in different scenarios confidently.

Example 1 – Display Array Elements

#include <stdio.h>

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

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

    return 0;
}

Output:

10 20 30 40 50

Explanation: Each element is accessed by its index (numbers[0] to numbers[4]) and printed individually. Arrays in C use zero-based indexing, meaning the first element has index 0, the second element has index 1, and so on.

Deep Explanation: In memory, these integers are stored in consecutive memory locations. Suppose numbers[0] is stored at memory address 2000, then numbers[1] will be at 2004 (since an int typically takes 4 bytes). This makes accessing array elements fast and efficient.

Example 2 – Sum of Array Elements

#include <stdio.h>

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

    sum = arr[0] + arr[1] + arr[2] + arr[3] + arr[4];

    printf("Sum = %d", sum);
    return 0;
}

Output:

Sum = 15

Explanation: We directly add each element without a loop and store the result in sum.

Deep Explanation: While this method works for small arrays, it becomes impractical for larger ones. Imagine an array of 1000 elements. You cannot write sum = arr[0] + arr[1] + ... + arr[999];. This is where loops come into play. Later in advanced lessons, we will use for loops to handle such cases easily.

Example 3 – Display Float Array Elements

#include <stdio.h>

int main() {
    float prices[4] = {12.5, 45.99, 7.25, 30.0};

    printf("%f ", prices[0]);
    printf("%f ", prices[1]);
    printf("%f ", prices[2]);
    printf("%f", prices[3]);

    return 0;
}

Output:

12.500000 45.990002 7.250000 30.000000

Explanation: %f prints float values with default six decimal places in C.

Deep Explanation: Even though we entered 45.99, the output shows 45.990002. This is due to the way floating-point numbers are stored in binary form, which can lead to tiny precision errors. This is a common concept in all programming languages, not just C.

Example 4 – Sum of Float Array Elements

#include <stdio.h>

int main() {
    float marks[3] = {88.5, 92.0, 75.25};
    float sum;

    sum = marks[0] + marks[1] + marks[2];

    printf("Total Marks = %f", sum);
    return 0;
}

Output:

Total Marks = 255.750000

Explanation: We directly add each float element without loops. %f displays the sum in default float format.

Deep Explanation: This example demonstrates how arrays can be used to store marks of students. Later, by using loops, we can calculate the sum, average, highest, and lowest marks of a whole class efficiently.

Example 5 – Display Char Array Elements

#include <stdio.h>

int main() {
    char vowels[5] = {'A', 'E', 'I', 'O', 'U'};

    printf("%c ", vowels[0]);
    printf("%c ", vowels[1]);
    printf("%c ", vowels[2]);
    printf("%c ", vowels[3]);
    printf("%c", vowels[4]);

    return 0;
}

Output:

A E I O U

Explanation: Here, we have created a char array to store English vowels and displayed them individually.

Deep Explanation: A char array is often used to store text or strings in C. For example, the word "HELLO" is actually stored as a char array: {'H','E','L','L','O','\0'}, where '\0' is the null character marking the end of the string.

Practice Statements

  1. Store and display three integers without loops.
  2. Add two integer array elements and print the result.
  3. Store and display three float values without loops.
  4. Add three float values and print the total.
  5. Store five vowels in a char array and print them one by one.

Additional Theory and Notes

Arrays provide many benefits:

  • Efficiency: Accessing elements by index is very fast.
  • Memory Management: Values are stored in continuous memory blocks.
  • Code Simplicity: Reduces the need for multiple variables.
  • Foundation: Arrays form the basis of advanced data structures.

However, arrays also have limitations:

  • Fixed Size: Once declared, the size cannot be changed.
  • Same Data Type: All elements must be of the same type.
  • No Bounds Checking: Accessing beyond the declared size may cause errors.

Frequently Asked Questions (FAQs)

  1. Q1: What is an array in C?
    An array is a collection of elements of the same type stored in contiguous memory locations, accessible using an index.
  2. Q2: What is the default index of the first element in a C array?
    The first element of an array always starts at index 0.
  3. Q3: Can we change the size of an array after declaration?
    No, arrays in C have a fixed size determined at compile time.
  4. Q4: What is the difference between an array and a normal variable?
    A variable stores a single value, while an array stores multiple values of the same type.
  5. Q5: How are arrays stored in memory?
    Arrays are stored in contiguous memory locations, making access efficient.
  6. Q6: Can we store different data types in a single array?
    No, all elements of an array must be of the same type.
  7. Q7: What happens if we access an index outside the array range?
    This causes undefined behavior and may lead to program crashes.
  8. Q8: How do we calculate the size of an array?
    We can use the formula sizeof(array) / sizeof(array[0]) to get the total number of elements.
  9. Q9: Can arrays be passed to functions?
    Yes, arrays can be passed to functions as arguments in C.
  10. Q10: What is the difference between a 1D and 2D array?
    A 1D array stores elements in a single row, while a 2D array stores elements in rows and columns like a table (matrix).