Skip to content
Justice Tech School

calloc and realloc

calloc gives zeroed memory and realloc resizes a block.

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

int main(void) {
  int *a = calloc(2, sizeof *a);
  int *b = realloc(a, 4 * sizeof *a);
  if (!b) { free(a); return 1; }
  b[3] = 9;
  printf("%d %d\n", b[0], b[3]);
  free(b);
  return 0;
}

Result

0 9

Exercise

Zeroed memory.

(n, size)

Quiz Test

What does calloc set memory to?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.