×

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures.

This includes data structures such as arrays.

An array is a collection of items or logically related variables of identical data type which are stored in contiguous memory locations. Once the size of array is declared, it cannot be changed and hence the size of array declared may not be sufficient enough for the task it was declared for. This may lead to unwanted termination of the program or not getting the desired outputs.

This problem can be resolved by allocating the memory during run time, this process has to be manual. This manual declaration of memory at run time is known as dynamic memory allocation and this is carried by a special set of functions known as library functions. The calloc() is a library function in C which is used for dynamic memory allocation.

The malloc() function also performs the task of memory allocation but the allocated memory is uninitialized.

Meaning

The term calloc is short for contiguous allocation and is a library function in C which allocates the memory which is requested and returns a pointer to it. The pointer is returned to the beginning of the block of memory.

Note: The block of memory allocated is set to zero, i.e., all bits are set to zero.

Declaration

void *calloc (n, size)

where, n = number of elements to be allocated

             size = Size of the elements

Eg: p = (int*) calloc(10, sizeof(int));  /*This statement allocates contiguous space in 

    memory for 10 elements of type int */

Program: C program for showing the usage of calloc() function

#include <stdio.h>
#include <stdlib.h>


int main () 
{
   int i, n;   //declaring the local variables
   int *p;


   printf("Enter number of elements : "); //number of elements required
   scanf("%d",&n); //scanning the input


   p = (int*)calloc(n, sizeof(int));  //declaring the calloc function


   printf("Enter the numbers here (%d numbers) :\n",n); //numbers entered by user
   for( i=0 ; i < n ; i++ ) {


      scanf("%d",&p[i]);
   }


   printf("The numbers entered are as follows : ");


   for( i=0 ; i < n ; i++ ) {


   printf("%d ",p[i]);
   }


   free( p );


   return(0);
}

Output of the following program:

Calloc in C

Conclusion

The calloc() function is a library function in C and it is used for allocation of space for complex data types such as an array. It allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory.

Note 1: Difference between malloc and calloc

              malloc() function allocates memory but does not set the memory to zero, i.e.,

             memory is uninitialized.

            calloc() function on the other hand allocates memory and set the memory to

             zero, all bits are initialized to zero.

Note 2: The calloc() function returns a pointer to the allocated memory, but if there is an error while allocating memory space like shortage of memory, then the return value is NULL.


Related Topics

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Structure Pointer in C

Structure Pointer in C In the C programming language, a structure pointer is defined as a pointer that points to the memory block address that stores a structure. Like C standard...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.