C <stdlib.h> library
The <stdlib.h>
header file in C is part of the C Standard Library and provides a range of functions for performing general utility operations. These operations include memory allocation, process control, conversions, and other miscellaneous tasks. The functions and macros defined in <stdlib.h>
are essential for effective programming in C.
Key Features of <stdlib.h>
- Memory Management: Functions for dynamic memory allocation and deallocation.
- Process Control: Functions for terminating processes and exiting programs.
- Conversions: Functions for converting strings to numeric values and vice versa.
- Utility Functions: Functions for sorting, searching, and generating random numbers.
Commonly Used Functions in <stdlib.h>
Here are some of the most commonly used functions and macros provided by <stdlib.h>
:
1. Memory Management Functions
malloc()
: Allocates a specified number of bytes and returns a pointer to the allocated memory. The memory is uninitialized.Syntax:
void *malloc(size_t size);
Example:
int *arr = (int *)malloc(10 * sizeof(int)); // Allocate memory for an array of 10 integers if (arr == NULL) { // Handle memory allocation failure }
calloc()
: Allocates memory for an array of specified elements, initializing all bytes to zero.Syntax:
void *calloc(size_t num, size_t size);
Example:
int *arr = (int *)calloc(10, sizeof(int)); // Allocate and initialize memory for 10 integers if (arr == NULL) { // Handle memory allocation failure }
realloc()
: Resizes previously allocated memory block. It can either expand or shrink the memory size.Syntax:
void *realloc(void *ptr, size_t new_size);
Example:
arr = (int *)realloc(arr, 20 * sizeof(int)); // Resize the memory block to hold 20 integers if (arr == NULL) { // Handle memory allocation failure }
free()
: Deallocates previously allocated memory, preventing memory leaks.Syntax:
void free(void *ptr);
Example:
free(arr); // Free the allocated memory
2. Process Control Functions
exit()
: Terminates the program and returns a status code to the operating system.Syntax:
void exit(int status);
Example:
exit(0); // Exit the program with success status
abort()
: Causes abnormal program termination and generates a core dump.Syntax:
void abort(void);
Example:
abort(); // Immediately terminate the program
3. Conversion Functions
atoi()
: Converts a string to an integer.Syntax:
int atoi(const char *str);
Example:
int num = atoi("123"); // Convert string to integer
atof()
: Converts a string to a floating-point number.Syntax:
double atof(const char *str);
Example:
double num = atof("123.45"); // Convert string to double
strtol()
: Converts a string to a long integer, providing better error handling.Syntax:
long strtol(const char *str, char **endptr, int base);
Example:
char *end; long num = strtol("12345", &end, 10); // Convert string to long
4. Utility Functions
rand()
: Generates a pseudo-random number.Syntax:
int rand(void);
Example:
int random_number = rand(); // Get a random number
srand()
: Seeds the random number generator with a specified value.Syntax:
void srand(unsigned int seed);
Example:
srand(time(NULL)); // Seed with current time for randomness
qsort()
: Sorts an array.Syntax:
void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *));
Example:
int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } int arr[] = {4, 2, 3, 1, 5}; qsort(arr, 5, sizeof(int), compare); // Sort the array
Using <stdlib.h>
To use the functions defined in <stdlib.h>
, you need to include the header file at the beginning of your C source file:
#include <stdlib.h>
Summary
<stdlib.h>
provides essential functions for memory management, process control, conversions, and utility operations.- Functions like
malloc()
,calloc()
,realloc()
, andfree()
are crucial for dynamic memory management. - Process control functions like
exit()
andabort()
allow for proper termination of programs. - Conversion functions like
atoi()
andatof()
facilitate converting between strings and numeric types. - Utility functions such as
rand()
andqsort()
provide random number generation and sorting capabilities.
Understanding and effectively using <stdlib.h>
is vital for writing efficient and robust C programs.