C stdlib.h Functions

General Utilities in the C Standard Library
<stdlib.h> collects C's general-purpose tools. It covers dynamic allocation, text-to-number conversion, process termination, environment access, generic sorting and searching, pseudo-random sequences, integer helpers, and conversions between multibyte and wide text.
This catalog groups all 39 tutorials by the job you need to perform. Each guide provides the exact declaration, preconditions, success and failure behavior, an Important warning, and three complete examples with visible output.
Contracts to recognize
| Contract | Typical functions | Core check |
|---|---|---|
| Owned allocation | malloc(), calloc(), realloc() | Check NULL and eventually call free(). |
| Checked parsing | strtol(), strtod() | Inspect endptr and errno. |
| Callback ordering | qsort(), bsearch() | Use a consistent three-way comparator. |
| Termination side effects | exit(), _Exit(), quick_exit() | Know which handlers and streams are processed. |
| Locale conversion | mbtowc(), wcstombs() | Set the intended locale and check the sentinel. |
Choose a Function by Job
Need dynamic storage?
Start with malloc(), use calloc() for zeroed bytes, resize safely with realloc(), and finish with free().
Need to parse input?
Choose strtol() for signed integers, strtoull() for wide unsigned values, or strtod() for floating-point text.
Need to order data?
Use qsort() to order an array. Once sorted by the same comparator, use bsearch() for lookup.
Need to end the process?
Use exit() for normal cleanup, quick_exit() for its dedicated handlers, or _Exit() for immediate termination.
Need character conversion?
Choose the single-character pair mbtowc()/wctomb() or the string pair mbstowcs()/wcstombs().
Jump to a category
How to Browse This Index
- Start from the operation. Memory ownership, parsing, termination, and locale conversion have different failure models.
- Match the exact type. The abs and div families differ by int, long, and long long; strto functions differ by destination range.
- Identify the sentinel. NULL, nonzero, -1, ERANGE, and no return each mean different things in different APIs.
- Read the Important box. It highlights the precondition most likely to cause undefined behavior, leaks, injection, or silent bad input.
- Run all three examples. The examples progress from the basic call to an edge case and a practical pattern.
Search All 39 Functions
Filter by a function name or task such as allocate, parse, terminate, sort, environment, random, or multibyte.
Process control
Terminate programs, register cleanup work, or invoke the host command processor.
| Function | Best for | Tutorial |
|---|---|---|
abort() | Terminates the program abnormally. | Open |
exit() | Ends the program normally. | Open |
_Exit() | Terminates without normal cleanup. | Open |
quick_exit() | Performs quick program termination. | Open |
atexit() | Registers a normal-termination callback. | Open |
at_quick_exit() | Registers a quick-termination callback. | Open |
system() | Passes a command to the host environment. | Open |
Memory
Create, resize, align, and release dynamic storage with explicit ownership.
Integer absolute value and division
Compute magnitudes or quotient-and-remainder pairs for standard integer widths.
| Function | Best for | Tutorial |
|---|---|---|
abs() | Returns the absolute value of an int. | Open |
labs() | Returns the absolute value of a long. | Open |
llabs() | Returns the absolute value of a long long. | Open |
div() | Computes integer quotient and remainder together. | Open |
ldiv() | Computes a long quotient and remainder. | Open |
lldiv() | Computes a long long quotient and remainder. | Open |
String to number
Convert text to numeric values, with strto* functions providing validation and range reporting.
| Function | Best for | Tutorial |
|---|---|---|
atof() | Converts text to double. | Open |
atoi() | Converts decimal text to int. | Open |
atol() | Converts decimal text to long. | Open |
atoll() | Converts decimal text to long long. | Open |
strtod() | Converts checked text to double. | Open |
strtof() | Converts checked text to float. | Open |
strtol() | Converts checked text to long. | Open |
strtold() | Converts checked text to long double. | Open |
strtoll() | Converts checked text to long long. | Open |
strtoul() | Converts checked text to unsigned long. | Open |
strtoull() | Converts checked text to unsigned long long. | Open |
Search and sort
Order arbitrary arrays and search sorted data through callback comparators.
Pseudo-random numbers
Generate and seed the standard deterministic pseudo-random sequence.
Environment
Read a named value from the host program environment.
Multibyte and wide text
Convert locale-dependent multibyte characters and strings to or from wide characters.
| Function | Best for | Tutorial |
|---|---|---|
mblen() | Measures one multibyte character. | Open |
mbtowc() | Converts one multibyte character to wchar_t. | Open |
wctomb() | Converts one wide character to multibyte form. | Open |
mbstowcs() | Converts a multibyte string to wide characters. | Open |
wcstombs() | Converts a wide string to multibyte text. | Open |
Key Takeaways
- Ownership must be explicit. Every successful dynamic allocation needs one clear owner and exactly one eventual release.
- Prefer checked conversion. The strto family exposes both the stopping point and range errors; atoi-style functions do not.
- Comparators are contracts. Consistent negative, zero, and positive results are essential for both sorting and binary search.
- Termination choices differ. exit(), quick_exit(), _Exit(), and abort() do not run the same handlers or process streams the same way.
- Locale affects multibyte text. Byte sequences cannot be interpreted correctly without knowing the active character locale.
Frequently Asked Questions
Start with abort()
Begin the complete stdlib.h function sequence.
18 people found this page helpful
