×

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

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.