C Programming Tutorial 0/65 lessons ~6 min read Lesson 24

    Arrays in C

    An array holds multiple values of one type in a single, contiguous block of memory.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    An array holds multiple values of one type in a single, contiguous block of memory. You choose the length when you declare it, then reach each slot with a zero-based index. That layout makes reads and updates fast and predictable.

    Purpose of this lesson

    Note: The sizeof trick stops working once an array argument decays to a pointer inside a function — pass the length explicitly in that case.

    Understanding the topic

    Declaring and filling arrays Creating an array means picking a type, a name, and how many slots you need — then optionally assigning starting values.

    • Declaration — tell the compiler the type and capacity so it can reserve space.
    • Initialization — supply starting values; any slot you skip is zero-filled.
    • When values are provided at once, the compiler can infer the length for you.
    • If you only initialize part of the array, the rest become zero.
    • Reading with an index — Pick any slot instantly with bracket notation — the index counts from zero.
    • Assigning a new value — Overwrite a slot by combining the index with the assignment operator.
    • Walking through an array — Use a loop to visit each position — forward, backward, or with a step size.
    • How many elements? — Divide the array's byte size by one element's byte size — but only where the array itself is in scope.
    TermDescription
    IndexPosition of an item, starting at 0 up to length − 1.
    Fixed lengthCapacity is chosen at declaration time in standard C arrays.
    Contiguous layoutItems sit next to each other in memory.
    Direct accessFetch or store with arr[i] in constant time.

    Step-by-step explanation

    1. Declaring and filling arrays — Creating an array means picking a type, a name, and how many slots you need — then optionally assigning starting values.
    2. Reading with an index — Pick any slot instantly with bracket notation — the index counts from zero.
    3. Assigning a new value — Overwrite a slot by combining the index with the assignment operator.
    4. Walking through an array — Use a loop to visit each position — forward, backward, or with a step size.
    5. How many elements? — Divide the array's byte size by one element's byte size — but only where the array itself is in scope.

    Syntax reference

    Syntax reference:

    c
    elementType name[count];
    elementType name[] = { v0, v1, ... };

    Informative example

    Example program:

    c
    #include <stdio.h>
    int main(void) {
    int scores[] = {90, 85, 78, 92, 88, 95};
    int len = sizeof(scores) / sizeof(scores[0]);
    for (int i = 0; i < len; i++) {
    printf("%d ", scores[i]);
    }
    return 0;
    }

    Output

    90 85 78 92 88 95

    Execution workflow

    1Arrays in C — step by step
    1 / 4

    Declaring and filling arrays

    Creating an array means picking a type, a name, and how many slots you need — then optionally assigning starting values.

    Code examples

    Reading with an index

    Pick any slot instantly with bracket notation — the index counts from zero.

    c
    #include <stdio.h>
    int main(void) {
    int values[5] = {10, 20, 30, 40, 50};
    printf("%d ", values[2]);
    printf("%d ", values[4]);
    printf("%d ", values[0]);
    return 0;
    }

    Output

    30 50 10

    Assigning a new value

    Overwrite a slot by combining the index with the assignment operator.

    c
    #include <stdio.h>
    int main(void) {
    int values[5] = {10, 20, 30, 40, 50};
    values[0] = 99;
    printf("%d", values[0]);
    return 0;
    }

    Output

    99

    Walking through an array

    Use a loop to visit each position — forward, backward, or with a step size.

    c
    #include <stdio.h>
    int main(void) {
    int values[5] = {10, 20, 30, 40, 50};
    printf("Forward:\n");
    for (int i = 0; i < 5; i++)
    printf("%d ", values[i]);
    printf("\nReverse:\n");
    for (int i = 4; i >= 0; i--)
    printf("%d ", values[i]);
    return 0;
    }

    Output

    Forward:
    10 20 30 40 50 
    Reverse:
    50 40 30 20 10

    How many elements?

    Divide the array's byte size by one element's byte size — but only where the array itself is in scope.

    c
    #include <stdio.h>
    int main(void) {
    int values[5] = {10, 20, 30, 40, 50};
    int count = sizeof(values) / sizeof(values[0]);
    printf("%d", count);
    return 0;
    }

    Output

    5

    Real-world use

    Related topics:

    • Multidimensional arrays for tables and grids
    • Pointers — closely tied to how arrays behave in expressions
    • Parameter passing when sharing arrays with functions

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • Reading uninitialized storage — behavior is undefined.
    • Dismissing compiler warnings instead of fixing root causes.
    • Ignoring NULL returns from malloc, fopen, and similar APIs.

    Hands-on exercise

    Practice problems:

    • Locate the largest value in an integer array
    • Add up every element and print the total
    • Reverse elements in place
    • Insert a value at a chosen index (shift right)
    • Remove an item and close the gap
    • Rotate elements left by k steps
    • Return dynamically allocated data from a helper
    • Sort numbers ascending with a simple algorithm
    • Find two indices whose values sum to a target

    Summary

    Arrays bundle same-type data in order, support indexed access, and are usually processed with loops; measure length with sizeof while the array variable is still in scope.

    Ready to mark this lesson complete?Track your journey across the entire course.