Stat Mania
  • C Programming
  • All Courses
  1. Arrays and Structs
  2. How to Save Loop Results in an Array in C
  • Course Home
  • Getting Started
    • C's "Hello, World!" Explained Line by Line (for R & Python Users)
    • How to Test a Small C Snippet Quickly
  • Variables and Data Types
    • Declaring Multiple Variables in One Line in C
    • float vs double in C: Precision, Memory, and When to Use Which
  • Operators and Input
    • Pre-Increment vs Post-Increment in C: ++i vs i++
    • Checking User Input in C: Is It Really a Number?
  • Making Decisions
    • Nested if in C, Explained with Real-Life Examples
    • Shortcut Ways to Write if in C: the Ternary Operator and More
    • The switch Statement in C: Syntax, break, Fall-Through and Examples
  • Loops
    • The do-while Loop in C: Run First, Check After
    • Why Use break in a while Loop If the Condition Already Stops It?
    • Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)
    • 5 while Loop Practice Problems in C (with Hints and Solutions)
    • 5 for Loop Practice Problems in C (with Hints and Solutions)
    • 5 Nested for Loop Practice Problems in C (with Hints and Solutions)
  • Arrays and Structs
    • How to Save Loop Results in an Array in C
    • Structs in C: Grouping Related Data (with Python and R Comparisons)
  • Random Numbers
    • srand(time(NULL)) in C: Why Random Numbers Repeat and How to Seed Them
    • Tossing a Die in C Until We Get a 6

On this page

  • A fixed-size array
  • When the number of values is unknown
  • Summary
  1. Arrays and Structs
  2. How to Save Loop Results in an Array in C

How to Save Loop Results in an Array in C

Declare a fixed-size array, fill it with a for loop using the loop index, and grow it with malloc() and realloc() when the number of values is not known in advance.
Code 5 min

By the end of this lesson you will

  • Fill a fixed-size array from a loop
  • Grow an array with malloc and realloc

Resources

  • Arrays
  • malloc

A for loop with index i from 0 to 4 writes i times i into results[i], filling five boxes with 0, 1, 4, 9 and 16.

A for loop writes i * i into results[i] on each pass, filling the array one slot at a time.

To save loop results in an array in C, we declare an array with a fixed size before the loop, then use the loop’s index variable to assign a value to each slot.

Unlike Python, a C array cannot grow with something like append(). We manage the indexing ourselves.

A fixed-size array

This program squares the numbers 0 to 4 inside a loop and stores each result:

#include <stdio.h>

#define SIZE 5   // fixed size of the array

int main(void) {
    // 1. Declare the array with a predefined size
    int results[SIZE];

    // 2. Use a loop to populate the array
    for (int i = 0; i < SIZE; i++) {
        results[i] = i * i;   // save the result at index i
    }

    // 3. Print the saved elements to check
    printf("Saved results:\n");
    for (int i = 0; i < SIZE; i++) {
        printf("results[%d] = %d\n", i, results[i]);
    }

    return 0;
}

Output (compiled with gcc -Wall -Wextra):

Saved results:
results[0] = 0
results[1] = 1
results[2] = 4
results[3] = 9
results[4] = 16
Warning

Stay inside the array. Valid indices run from 0 to SIZE - 1, so the loop condition must be i < SIZE, not i <= SIZE. Writing outside the array is undefined behaviour: it may crash, or silently corrupt other variables.

When the number of values is unknown

If we do not know in advance how many times the loop will run (for example, reading numbers until the user types 0), a fixed-size array does not work. We allocate memory with malloc() and enlarge it with realloc() when it fills up:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int capacity = 2, count = 0, x;
    int *data = malloc(capacity * sizeof *data);
    if (data == NULL) return 1;

    printf("Enter numbers (0 to stop): ");
    while (scanf("%d", &x) == 1 && x != 0) {
        if (count == capacity) {          // full: double the space
            capacity *= 2;
            int *tmp = realloc(data, capacity * sizeof *data);
            if (tmp == NULL) { free(data); return 1; }
            data = tmp;
        }
        data[count++] = x;                // save, then move to the next slot
    }

    printf("Saved %d values:", count);
    for (int i = 0; i < count; i++) printf(" %d", data[i]);
    printf("\n");

    free(data);
    return 0;
}

Entering 4 8 15 16 23 42 0 gives:

Enter numbers (0 to stop): Saved 6 values: 4 8 15 16 23 42

Three habits to notice:

  • count tracks how many slots are used, and doubles as the next index.
  • realloc() goes into a temporary pointer first, so we do not lose data if it fails.
  • free() returns the memory when we are done.

Summary

Situation Approach
Number of iterations known Fixed array, array[i] = value;
Number of iterations unknown malloc() + realloc(), track count and capacity
  • Use the loop index to store each result: a[i] = ....
  • A fixed array needs its size known up front.
  • malloc and realloc grow storage at run time.

Index. A number that picks one element of an array; the first is 0.

Back to top
Stat Mania

Free, hands-on statistics and programming courses that run right in your browser.

Courses

All courses R Programming Statistics Probability

Resources

Practice quizzes Tools & games Quarto docs Course kit on GitHub

Company

Home About Contact Privacy policy Terms of use
© Stat Mania. All rights reserved.
Arrays and Structs
Structs in C: Grouping Related Data (with Python and R Comparisons)