The C calloc() function allocates zero-initialized array storage. It requests count multiplied by size bytes and initializes every byte to zero. Implementations must account for multiplication overflow rather than allocating a wrapped, undersized block.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. Success returns a pointer to zero-filled storage suitably aligned for ordinary object types. Failure returns NULL; do not read or write through it. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete calloc() call:
C
calloc(5, sizeof(int));
Call
What it does
calloc(5, sizeof(int));
Allocates zero-initialized array storage using the concrete values shown.
Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.
Library declaration
C
void *calloc(size_t count, size_t size);
Parameters
Parameter
Description
count
Number of array elements.
size
Size of each element in bytes.
Return Value
Return Value
Description
Non-null pointer
Allocation succeeded and all bits in the block are zero.
NULL
Allocation failed or the requested product cannot be represented.
C Examples
Example 1 — Allocate an integer array
This complete program demonstrates a distinct part of the calloc() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = calloc(4, sizeof *p);
if (!p) return EXIT_FAILURE;
p[2]=15;
printf("%d\n", p[2]);
free(p); return 0;
}
Output
15
How It Works
Step 1. Calculate or state the requested storage size.
Step 2. Call calloc() and test for NULL.
Step 3. Initialize the element before reading it, even though calloc supplied zero bytes.
Step 4. Release the allocation after its final use.
Example 2 — Fill every element
This complete program demonstrates a distinct part of the calloc() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = calloc(4, sizeof *p);
if (!p) return EXIT_FAILURE;
for (int i=0;i<4;i++) p[i]=(i+1)*10;
printf("%d %d\n", p[0], p[3]);
free(p); return 0;
}
Output
10 40
How It Works
Step 1. Obtain storage for four values.
Step 2. Stop immediately if allocation failed.
Step 3. Initialize each element within bounds.
Step 4. Print two values, then free the block.
Example 3 — Handle allocation failure
This complete program demonstrates a distinct part of the calloc() contract and checks the result or precondition that matters for the scenario.
Step 4. Return a failure status when storage is unavailable.
Frequently Asked Questions
calloc() allocates zero-initialized array storage. It requests count multiplied by size bytes and initializes every byte to zero. Implementations must account for multiplication overflow rather than allocating a wrapped, undersized block.
Include <stdlib.h> before calling calloc().
Success returns a pointer to zero-filled storage suitably aligned for ordinary object types. Failure returns NULL; do not read or write through it.
Zeroed bytes are not a portable promise that every pointer or floating value has its semantic zero representation. Always check for NULL and release with free().
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.