free() Function in C Library With Examples Last Updated : 29 May, 2023 Comments Improve Suggest changes Like Article Like Report The free() function in C is used to free or deallocate the dynamically allocated memory and helps in reducing memory wastage. The C free() function cannot be used to free the statically allocated memory (e.g., local variables) or memory allocated on the stack. It can only be used to deallocate the heap memory previously allocated using malloc(), calloc() and realloc() functions. The free() function is defined inside <stdlib.h> header file. C free() FunctionSyntax of free() Function in Cvoid free(void *ptr);Parametersptr is the pointer to the memory block that needs to be freed or deallocated.Return ValueThe free() function does not return any value.Examples of free()Example 1: The following C program illustrates the use of the calloc() function to allocate memory dynamically and free() function to release that memory. C // C program to demonstrate use of // free() function using calloc() #include <stdio.h> #include <stdlib.h> int main() { // This pointer ptr will hold the // base address of the block created int* ptr; int n = 5; // Get the number of elements for the array printf("Enter number of Elements: %d\n", n); scanf("%d", &n); // Dynamically allocate memory using calloc() ptr = (int*)calloc(n, sizeof(int)); // Check if the memory has been successfully // allocated by calloc() or not if (ptr == NULL) { printf("Memory not allocated \n"); exit(0); } // Memory has been Successfully allocated using calloc() printf("Successfully allocated the memory using " "calloc(). \n"); // Free the memory free(ptr); printf("Calloc Memory Successfully freed."); return 0; } OutputEnter number of Elements: 5 Successfully allocated the memory using calloc(). Calloc Memory Successfully freed.Example 2: The following C program illustrates the use of the malloc() function to allocate memory dynamically and free() function to release that memory. C // C program to demonstrate use of // free() function using malloc() #include <stdio.h> #include <stdlib.h> int main() { // This pointer ptr will hold the // base address of the block created int* ptr; int n = 5; // Get the number of elements for the array printf("Enter number of Elements: %d\n", n); scanf("%d", &n); // Dynamically allocate memory using malloc() ptr = (int*)malloc(n * sizeof(int)); // Check if the memory has been successfully // allocated by malloc() or not if (ptr == NULL) { printf("Memory not allocated \n"); exit(0); } // Memory has been Successfully allocated using malloc() printf("Successfully allocated the memory using " "malloc(). \n"); // Free the memory free(ptr); printf("Malloc Memory Successfully freed."); return 0; } OutputEnter number of Elements: 5 Successfully allocated the memory using malloc(). Malloc Memory Successfully freed. Comment More infoAdvertise with us Next Article Predefined Macros in C with Examples K kamleshjoshi18 Follow Improve Article Tags : C Language C-Dynamic Memory Allocation c-memory-management Similar Reads C Library math.h Functions The math.h header defines various C mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result. Let us discuss some important C math functions one by one. C Math Functions1. double ceil (double x) The C library functio 6 min read User-Defined Function in C A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User-defined functions are different from built-in functions as their working is specified by the user and no hea 6 min read Predefined Macros in C with Examples According to C89 Standard, C programming language has following predefined macros: __LINE__ Macro: __LINE__ macro contains the current line number of the program in the compilation. It gives the line number where it is called. It is used in generating log statements, error messages, throwing excepti 4 min read Predefined Macros in C with Examples According to C89 Standard, C programming language has following predefined macros: __LINE__ Macro: __LINE__ macro contains the current line number of the program in the compilation. It gives the line number where it is called. It is used in generating log statements, error messages, throwing excepti 4 min read Predefined Macros in C with Examples According to C89 Standard, C programming language has following predefined macros: __LINE__ Macro: __LINE__ macro contains the current line number of the program in the compilation. It gives the line number where it is called. It is used in generating log statements, error messages, throwing excepti 4 min read How Do I Create a Library in C? Libraries are a collection of code that can be used by other programs. They promote code reuse and modularity. In C, we can create our own libraries. In this article, we will learn how to create a library in C. Creating a Library in CIn C, there are multiple methods using which we can create a libra 3 min read Like