Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Standard Library: stdlib.h

Lesson 19 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Standard Library: stdlib.h

The stdlib.h header provides general-purpose functions for memory management, type conversions, random numbers, and program control.

---

Memory Management

FunctionPrototypeDescription
mallocvoid *malloc(size_t size)Allocate uninitialised memory
callocvoid *calloc(size_t n, size_t size)Allocate zero-initialised memory
reallocvoid realloc(void ptr, size_t size)Resize allocated memory
freevoid free(void *ptr)Release allocated memory
int *arr = (int *)malloc(5 * sizeof(int));
int *zeros = (int *)calloc(5, sizeof(int));

arr = (int *)realloc(arr, 10 * sizeof(int));
free(arr);

---

Type Conversion Functions

FunctionDescriptionExample
atoiString to intatoi("42") → 42
atofString to doubleatof("3.14") → 3.14
atolString to longatol("100") → 100L
itoaInt to string (non-standard)itoa(42, buf, 10)
char str[] = "123";
int n = atoi(str);
printf("%d\n", n + 1);   /* 124 */

---

Random Number Generation

#include <stdlib.h>
#include <time.h>

srand(time(NULL));          /* seed with current time */
int r = rand();             /* random int 0 to RAND_MAX */
int dice = rand() % 6 + 1; /* random 1 to 6 */

---

Sorting and Searching

/* qsort */
int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

int arr[] = {5, 2, 8, 1, 9};
qsort(arr, 5, sizeof(int), compare);
/* arr is now: 1 2 5 8 9 */

/* bsearch (array must be sorted) */
int key = 5;
int *result = (int *)bsearch(&key, arr, 5, sizeof(int), compare);
if (result) printf("Found: %d\n", *result);

---

Program Control

FunctionDescription
exit(0)Normal program termination
exit(1)Abnormal termination
abort()Immediate abnormal termination
system("cmd")Execute a system command
getenv("VAR")Get environment variable
#include <stdlib.h>

int main() {
    system("cls");           /* clear screen (Windows) */
    printf("Path: %s\n", getenv("PATH"));
    exit(0);
}