calloc in C: solved exercise with zero-initialized dynamic array

calloc in C: solved exercise step by step

If you searched for a solved calloc exercise in C, here is the practical difference between calloc and malloc, and when to use each one.

calloc allocates memory for an array of n elements of size bytes each and initializes all of them to zero, unlike malloc which leaves the content undefined.

calloc signature

1
void *calloc(size_t n, size_t size);
  • n — number of elements.
  • size — size in bytes of each element.
  • Returns a pointer to the allocated block, or NULL on failure.

Problem statement

  1. Allocate a dynamic array of 5 integers with calloc and print the initial values (all must be 0).
  2. Fill it with the squares of 1 to 5 and print the result.
  3. Free the memory with free.
  4. Then allocate the same array with malloc and observe that the initial values are undefined.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = 5;

    /* --- calloc: memory initialized to zero --- */
    int *arr_calloc = calloc(n, sizeof(int));
    if (arr_calloc == NULL) {
        fprintf(stderr, "Error: calloc failed\n");
        return 1;
    }

    printf("Initial values with calloc:\n");
    for (int i = 0; i < n; i++) {
        printf("arr_calloc[%d] = %d\n", i, arr_calloc[i]);
    }

    for (int i = 0; i < n; i++) {
        arr_calloc[i] = (i + 1) * (i + 1);
    }

    printf("\nAfter filling:\n");
    for (int i = 0; i < n; i++) {
        printf("arr_calloc[%d] = %d\n", i, arr_calloc[i]);
    }

    free(arr_calloc);

    /* --- malloc: memory NOT initialized --- */
    int *arr_malloc = malloc(n * sizeof(int));
    if (arr_malloc == NULL) {
        fprintf(stderr, "Error: malloc failed\n");
        return 1;
    }

    printf("\nInitial values with malloc (undefined):\n");
    for (int i = 0; i < n; i++) {
        printf("arr_malloc[%d] = %d\n", i, arr_malloc[i]);
    }

    free(arr_malloc);
    return 0;
}

Expected output

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Initial values with calloc:
arr_calloc[0] = 0
arr_calloc[1] = 0
arr_calloc[2] = 0
arr_calloc[3] = 0
arr_calloc[4] = 0

After filling:
arr_calloc[0] = 1
arr_calloc[1] = 4
arr_calloc[2] = 9
arr_calloc[3] = 16
arr_calloc[4] = 25

Initial values with malloc (undefined):
arr_malloc[0] = <garbage_value>
...

malloc vs calloc vs realloc

FunctionInitializes to 0ResizesTypical use
malloc(n)NoNotemporary buffer, single struct
calloc(n, size)YesNoarrays where zero is meaningful
realloc(ptr, n)NoYesarrays that grow dynamically

Common mistakes

  • Not checking if calloc returns NULL (out of memory).
  • Forgetting free, causing a memory leak.
  • Assuming malloc initializes to zero: it never guarantees that.
  • Passing parameters in the wrong order: first n (count), then size (bytes per element).

Practical use

calloc is preferred over malloc when:

  • a zero initial value is meaningful (counters, empty matrices, byte buffers),
  • you want to avoid accidentally reading garbage during development.

Guided practice and next step

If you want a complete path with progressive difficulty:

FAQ

Is calloc slower than malloc?

On most systems calloc can be as fast or even faster than malloc + memset because the OS may supply already-zeroed pages. In practice the difference is negligible for small arrays.

Can I use realloc on memory allocated with calloc?

Yes. realloc works with any pointer obtained from malloc, calloc, or a previous realloc call. The new block is not zero-initialized.

When is calloc strictly required?

When code assumes array elements are zero before any write. If you always write before reading, malloc is sufficient.