Calloc() Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

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.

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.

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.