The <stdlib.h> header defines four variable types, several macros, and various functions for performing general functions. It is most famous for Dynamic Memory Allocation and program control.
Unlike regular arrays, memory allocated via stdlib.h functions happens on the Heap. This allows you to determine the size of your data at runtime.
These functions are essential for converting strings (often from user input) into numeric values that the computer can calculate.
| Function | Conversion Type |
|---|---|
atoi() |
ASCII to Integer. |
atof() |
ASCII to Float. |
strtol() |
String to Long (More robust than atoi). |
Functions that interact with the operating system or handle the program's lifecycle.
Instead of writing your own sorting algorithm, stdlib.h provides highly optimized versions of common algorithms.
As covered in previous modules, stdlib.h handles the generation of pseudo-random numbers.
rand() / srand(): Random number generation.abs(): Returns the absolute value of an integer.div(): Returns both the quotient and remainder of a division.Understanding which to use is a common interview question:
free(ptr). This prevents "Dangling Pointers," which are a major cause of program crashes and security bugs!