C stdlib.h Library

C stdlib.h Library

The stdlib.h library in C stands for Standard Library. It provides general-purpose functions for:

  • Memory management

  • Program control

  • Conversions

  • Random numbers

  • Searching and sorting

It is one of the most commonly used standard libraries in C.


📌 Common Categories & Functions

1️⃣ Memory Management

Function Description
malloc(size_t size) Allocates memory dynamically, returns pointer
calloc(size_t n, size_t size) Allocates zero-initialized memory
realloc(void *ptr, size_t size) Resizes previously allocated memory
free(void *ptr) Frees allocated memory

Example:

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

int main() {
int *arr;
int n = 5;

arr = (int*)malloc(n * sizeof(int));
if(arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

for(int i=0; i<n; i++) arr[i] = i+1;

for(int i=0; i<n; i++) printf("%d ", arr[i]);

free(arr); // free memory
return 0;
}


2️⃣ Program Control

Function Description
exit(int status) Terminates program immediately
abort() Aborts program
atexit(void (*func)(void)) Registers a function to run at program exit

Example:

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

void goodbye() {
printf("Program ended\n");
}

int main() {
atexit(goodbye); // register exit function
printf("Hello\n");
exit(0);
}


3️⃣ Random Numbers

Function Description
rand() Returns pseudo-random number
srand(unsigned int seed) Sets seed for rand()

Example:

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

int main() {
srand(time(0)); // initialize random seed
int random = rand() % 100; // random number 0-99
printf("Random Number: %d\n", random);
return 0;
}


4️⃣ String Conversion

Function Description
atoi(const char *str) Convert string to integer
atof(const char *str) Convert string to float
atol(const char *str) Convert string to long
strtol, strtod Advanced conversion with error checking

Example:

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

int main() {
char numStr[] = "1234";
int num = atoi(numStr);
printf("Integer: %d\n", num);
return 0;
}


5️⃣ Searching and Sorting

Function Description
qsort(void *base, size_t nitems, size_t size, int (*compar)(const void*,const void*)) Sorts an array
bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void*,const void*)) Binary search in sorted array

Example: Sorting integers

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

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

int main() {
int arr[] = {5, 2, 8, 1, 9};
int n = sizeof(arr)/sizeof(arr[0]);

qsort(arr, n, sizeof(int), compare);

for(int i=0;i<n;i++) printf("%d ", arr[i]);
return 0;
}


6️⃣ Dynamic Memory + Struct Example

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

struct Student {
char name[50];
int marks;
};

int main() {
int n = 2;
struct Student *students = malloc(n * sizeof(struct Student));

for(int i=0;i<n;i++) {
printf("Enter name and marks: ");
scanf("%s %d", students[i].name, &students[i].marks);
}

for(int i=0;i<n;i++) {
printf("%s - %d\n", students[i].name, students[i].marks);
}

free(students);
return 0;
}


📌 Summary Table

Category Functions
Memory Management malloc, calloc, realloc, free
Program Control exit, abort, atexit
Random Numbers rand, srand
String Conversion atoi, atof, atol, strtol, strtod
Searching & Sorting qsort, bsearch

✅ Notes

  1. Always check the return of malloc/calloc/realloc for NULL.

  2. Use free() to avoid memory leaks.

  3. rand() generates pseudo-random numbers, use srand() to seed.

  4. qsort() and bsearch() are powerful for arrays of structs or complex data.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *