
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is Calloc in C language?
The C library memory allocation function void *calloc(size_t nitems, size_t size) allocates the requested memory and returns a pointer to it.
The difference in malloc and calloc is that malloc does not set the memory to zero, whereas, calloc sets the allocated memory to zero.
Memory allocation Functions
Memory can be allocated in two ways as explained below −
Once memory is allocated at compile time, it cannot be changed during execution. There will be a problem of either insufficiency or else wastage of memory.
The solution is to create memory dynamically i.e. as per the requirement of the user during execution of program.
The standard library functions which are used for dynamic memory management are as follows −
- malloc ( )
- calloc ( )
- realloc ( )
- free ( )
The Calloc ( ) function
This function is used for allocating continuous blocks of memory at run time.
This is specially designed for arrays.
It returns a void pointer, which points to the base address of the allocated memory.
The syntax for calloc() function is given below −
void *calloc ( numbers of elements, size in bytes)
Example
The following example shows the usage of calloc() function.
int *ptr; ptr = (int * ) calloc (500,2);
Here, 500 blocks of memory each of size 2 bytes will be allocated continuously. Total memory allocated = 1000 bytes.
int *ptr; ptr = (int * ) calloc (n, sizeof (int));
Example program
Given below is a C Program to compute sum of even numbers and odd numbers in a set of elements using dynamic memory allocation functions Calloc.
#include<stdio.h> #include<stdlib.h> void main(){ //Declaring variables, pointers// int i,n; int *p; int even=0,odd=0; //Declaring base address p using Calloc// p = (int * ) calloc (n, sizeof (int)); //Reading number of elements// printf("Enter the number of elements : "); scanf("%d",&n); /*Printing O/p - We have to use if statement because we have to check if memory has been successfully allocated/reserved or not*/ if (p==NULL){ printf("Memory not available"); exit(0); } //Storing elements into location using for loop// printf("The elements are :
"); for(i=0;i<n;i++){ scanf("%d",p+i); } for(i=0;i<n;i++){ if(*(p+i)%2==0){ even=even+*(p+i); } else { odd=odd+*(p+i); } } printf("The sum of even numbers is : %d
",even); printf("The sum of odd numbers is : %d
",odd); }
Output
When the above program is executed, it produces the following result −
Enter the number of elements : 4 The elements are : 12 56 23 10 The sum of even numbers is : 78 The sum of odd numbers is : 23
- Related Articles
- calloc() versus malloc() in C
- What is malloc in C language?
- What is Realloc in C language?
- What is C++ programming language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is an identifier in C language?
- What is void pointer in C language?
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
