free() Function in C Library With Examples Last Updated : 29 May, 2023 Summarize Comments Improve Suggest changes Share 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 free() Function in C Library 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 getdate() and setdate() function in C with Examples setdate() Method: getdate() function is defined in dos.h header file. This function fills the date structure *dt with the system's current date. Syntax struct date dt; getdate(&dt); Parameter: This function accepts a single parameter dt which is the object of structure date. Return Value: This 2 min read Formatted and Unformatted Input/Output functions in C with Examples In C language, the Input/Output (I/O) functions are part of the standard library, and these functions are used for interacting with the user or other systems, to perform operations such as reading input and printing output. These functions provide ways to read data from files and other input devices 7 min read ctype.h(<cctype>) library in C/C++ with Examples As string.h header file contains inbuilt functions to handle Strings in C/C++, the ctype.h/<cctype> contains inbuilt functions to handle characters in C/C++ respectively. Characters are of two types: Printable Characters: The characters that are displayed on the terminal. Control Characters: T 5 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 Like