C stdlib.h

<stdlib.h> is a standard C header for many general-purpose utilities.

It includes functions for dynamic memory, number conversion, program control, searching, sorting, and pseudo-random numbers.

Include stdlib.h

#include <stdlib.h>

The header name comes from standard library.

It provides declarations, types, and macros needed by several general utility functions.

What stdlib.h provides in C The stdlib.h header connects to dynamic memory functions, numeric conversion functions, program control functions, and utility functions such as rand and qsort. stdlib.h → GENERAL C UTILITIES STANDARD HEADER <stdlib.h> MEMORY malloc() calloc() free() CONVERSION strtol() strtod() PROGRAM CONTROL exit() abort() UTILITIES rand() qsort() KEY IDEA stdlib.h groups many general-purpose standard library tools. Include the header that declares the function or macro you use.

Dynamic Memory Functions

int *number = malloc(sizeof *number); 

if (number != NULL) { 
    *number = 42; 
    free(number); 
}

<stdlib.h> declares malloc(), calloc(), realloc(), and free().

String-to-Number Conversion

char *end; 
long value = strtol("123", &end, 10);

strtol() converts text to a long and lets your program inspect where conversion stopped.

This makes it more useful for checked input than simpler functions such as atoi().

Program Control

exit(EXIT_SUCCESS);

exit() ends the program normally.

EXIT_SUCCESS and EXIT_FAILURE are macros provided through <stdlib.h>.

abort()

abort();

abort() causes abnormal program termination.

It is not a replacement for ordinary error handling.

Pseudo-Random Numbers

int value = rand();

rand() returns a pseudo-random integer between 0 and RAND_MAX, inclusive.

srand() sets the starting seed for the pseudo-random sequence.

Searching and Sorting

<stdlib.h> also declares general-purpose functions such as:

C stdlib.h Example

#include <stdio.h> 
#include <stdlib.h> 

int main(void) { 
    int *number = malloc(sizeof *number); 

    if (number == NULL) { 
        return EXIT_FAILURE; 
    } 

    *number = 42; 

    printf("Value: %d\n", *number); 

    free(number); 

    return EXIT_SUCCESS; 
}

Output

Value: 42

This example uses malloc(), free(), EXIT_FAILURE, and EXIT_SUCCESS from <stdlib.h>.

stdlib.h vs stdio.h

stdlib.h stdio.h
General utilities Input and output
malloc(), free() printf(), scanf()
strtol(), exit() fopen(), fgets()

Common stdlib.h Mistakes

Mistake Correct Idea
Using malloc() without including <stdlib.h>. Include the header that declares the function.
Forgetting to check malloc() for NULL. Check allocation success before dereferencing.
Using atoi() when invalid input must be detected. Prefer a checked conversion such as strtol().
Thinking rand() provides cryptographically secure random values. rand() is only a basic pseudo-random generator.
  1. Which header declares malloc() and free()?
  2. Which function can convert a string to long with error checking support?
  3. Which macro can represent successful program termination?

Practice Answers

1. Which header declares malloc() and free()?

Use <stdlib.h>.

2. Which checked conversion function?

A common choice is strtol().

3. Which success macro?

Use EXIT_SUCCESS.

FAQs

What is stdlib.h in C?

stdlib.h is a standard C header that declares general utility functions for tasks such as dynamic memory allocation, numeric conversion, program termination, searching and sorting, and pseudo-random numbers.

Which memory functions are declared in stdlib.h?

stdlib.h declares malloc(), calloc(), realloc(), and free().

Which function is better for checked string-to-integer conversion?

strtol() is usually a better choice than atoi() when you need to detect invalid input or conversion errors.

Does stdlib.h provide exit()?

Yes. stdlib.h declares exit(), along with other program-control functions such as abort().

Summary

<stdlib.h> provides many general-purpose C utilities. Important groups include dynamic memory functions such as malloc() and free(), conversion functions such as strtol(), program-control functions such as exit(), and utilities such as rand(), qsort(), and bsearch().