C stdlib.h Functions

Beginner
14 min read
Updated: Nov 2026
39 tutorials
<stdlib.h>

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

ContractTypical functionsCore check
Owned allocationmalloc(), calloc(), realloc()Check NULL and eventually call free().
Checked parsingstrtol(), strtod()Inspect endptr and errno.
Callback orderingqsort(), bsearch()Use a consistent three-way comparator.
Termination side effectsexit(), _Exit(), quick_exit()Know which handlers and streams are processed.
Locale conversionmbtowc(), 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().

Need a magnitude or remainder?

Match abs(), labs(), or llabs() to the integer width; use the corresponding div family for quotient plus remainder.

Jump to a category

How to Browse This Index

  1. Start from the operation. Memory ownership, parsing, termination, and locale conversion have different failure models.
  2. Match the exact type. The abs and div families differ by int, long, and long long; strto functions differ by destination range.
  3. Identify the sentinel. NULL, nonzero, -1, ERANGE, and no return each mean different things in different APIs.
  4. Read the Important box. It highlights the precondition most likely to cause undefined behavior, leaks, injection, or silent bad input.
  5. 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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
malloc()Allocates a block of dynamic storage.Open
calloc()Allocates zero-initialized array storage.Open
realloc()Changes the size of an allocation.Open
free()Releases dynamically allocated storage.Open
aligned_alloc()Allocates dynamically aligned storage.Open

Integer absolute value and division

Compute magnitudes or quotient-and-remainder pairs for standard integer widths.

FunctionBest forTutorial
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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
bsearch()Searches a sorted array efficiently.Open
qsort()Sorts an array in place.Open

Pseudo-random numbers

Generate and seed the standard deterministic pseudo-random sequence.

FunctionBest forTutorial
rand()Produces a pseudo-random integer.Open
srand()Seeds the rand pseudo-random sequence.Open

Environment

Read a named value from the host program environment.

FunctionBest forTutorial
getenv()Reads an environment variable.Open

Multibyte and wide text

Convert locale-dependent multibyte characters and strings to or from wide characters.

FunctionBest forTutorial
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

It declares general-purpose utilities for dynamic memory, numeric conversion, process termination, sorting, searching, pseudo-random numbers, the environment, and multibyte conversion.
Start with malloc(), calloc(), realloc(), and free(). Learn aligned_alloc() only when an explicit alignment is genuinely required.
Usually no. Prefer strtol() because endptr and errno let you separate valid conversion from no digits, trailing text, and range failure.
Yes, both use negative, zero, and positive comparator results. bsearch() additionally requires the array to already be sorted by the same ordering.
No. rand() is predictable and intended for simple simulations and demonstrations, not keys, passwords, tokens, or security decisions.

Start with abort()

Begin the complete stdlib.h function sequence.

abort() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful