C calloc() Function

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is calloc() Function?

void *calloc(size_t count, size_t size) allocates room for count elements of size bytes each and sets every byte to zero. It also checks that count * size does not overflow, which makes it the safer choice for arrays.

Remember
int *a = calloc(n, sizeof *a);   /* n ints, all zero */
if (!a) { /* failure */ }
free(a);

Cheat Sheet

Keep these facts handy while you work with calloc() Function in C.

TopicDetail
Header<stdlib.h>
ZeroingAll bytes set to 0
Overflowcount * size is checked
Pointers/floatsAll-bits-zero is 0 on common platforms

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: realloc() Function.

Example 1 — Zero-filled counters

Count letters without initializing the array manually.

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

int main(void) {
    int *count = calloc(3, sizeof *count);
    if (!count) return 1;

    const char *word = "abca";
    for (const char *p = word; *p; p++) {
        if (*p >= 'a' && *p <= 'c') count[*p - 'a']++;
    }
    printf("a=%d b=%d c=%d\n", count[0], count[1], count[2]);

    free(count);
    return 0;
}

How It Works

  1. calloc starts every counter at 0.
  2. *p - 'a' converts a letter to index 0..2.
  3. Finally the array is freed.

Edge Cases & Pitfalls

Check these before relying on calloc() Function in real programs.

speed

Zeroing large blocks

Zeroing costs time. Use malloc when you will overwrite everything anyway.

null

Assuming 0.0 for doubles

All-bits-zero is 0.0 and NULL on mainstream platforms, but the standard only guarantees it for integers.

Key Takeaways

  • Zeroed: safe starting state.
  • Two arguments: count and size.
  • Overflow-safe: preferred for arrays.

One line: void *calloc(size_t count, size_t size) allocates room for count elements of size bytes each and sets every byte to zero.

Frequently Asked Questions

calloc takes a count and element size, zeroes the memory, and checks for multiplication overflow. malloc takes a total size and leaves the memory uninitialized.
It can be, because it must clear the memory, though some systems get zeroed pages for free.
Continue with "C realloc() Function" next. Resize a block while keeping its existing contents. The C Dynamic Memory Allocation hub lists every topic in order.

Next: realloc() Function

Resize a block while keeping its existing contents.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful