Trusted answers to developer questions

What is Calloc in C?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

The calloc() function in C is used to allocate a specified amount of memory and then initialize it to zero. The function returns a void pointer to this memory location, which can then be cast to the desired type. The function takes in two parameters that collectively specify the amount of memory ​​to be allocated.

svg viewer

Code​

Take a look at the code below. Note how (int*) is used to convert the void pointer to an​ int pointer.

#include<stdio.h>
#include<stdlib.h>
int main() {
int* a = (int*) calloc(5, sizeof(int));
return 0;
}

RELATED TAGS

calloc
memory allocation
c
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?