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

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.