Calloc() Function in C

Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Overview
Sometimes we need to allocate the memory for variables in the run time. There are multiple functions for dynamic memory allocation in C which solves different purposes. We have calloc function in C programming language with which we can dynamically allocate multiple blocks of memory with the given size for each block.
What is calloc() Function in C?
The calloc in C is used for the dynamic memory allocation. With the help of calloc function, we can allocate multiple blocks of memory with the same size for each block. It is defined inside stdlib.h header file. calloc() function set each memory block to zero initially.
Syntax of calloc() Function in C
- Above code snippet shows the syntax of calloc() function in C.
- cast_type* represents the datatype that we are using for type casting.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Parameters of calloc() Function in C
calloc() function take two parameters -
- number_of_block represents the number of blocks it will allocate.
- size_of_block represents the size of one single block.
Return Value of calloc() Function in C
The calloc in C returns the void* (void pointer) on successful memory allocation which points to the starting of the memory blocks allocated by it, this void pointer can be cast to the desired type of data pointer to be dereferenceable.
If the calloc() function fails to allocate the requested block of memory then we get null pointer as a return value.
Turn Learning into Career Growth
Example of calloc() Function in C
Following is an example of calloc in C where we created the array of 5 blocks, where each block is of size 4 bytes(size of integer).
Code Example -
Output -
In the above code, we declare ptr int* pointer and using calloc() allocate 5 blocks of int type which is then pointed by ptr. Then we assigned a value to each block and print it at the end.
More Examples
The main advantage of calloc in C programming language can be seen when we want to create an array of custom datatype structures.
Output:
Here in the above coding example, we have the student structure where we have the roll number and name as a variable in the structure. With the help of the calloc function, we have allocated 3 blocks of memory, each block of the size of the student structure.
Note- To deallocate the memory we use free() function as the dynamically allocated memory does not deallocate automatically like static memory.
Conclusion
- The calloc() function in C language is used for dynamic memory allocation.
- We can allocate multiple blocks with the same given size of memory for each block using calloc in c.
- The calloc() function returns the void* on the successful memory allocation.
- The calloc() function can be utilized to create an array of structures.




