Stdlib.h in C
×


Stdlib.h in C

45

The stdlib.h header file in C provides a collection of functions that are essential for various tasks such as memory allocation, random number generation, string manipulation, and environment control.

Memory Allocation Functions

1malloc(size_t size):

Allocates a block of memory of the specified size and returns a pointer to the beginning of the block.

Syntax:

void *malloc(size_t size);
Example:

// Program for malloc in C
#include<stdio.h> 
#include<stdlib.h> 

int main() {
    int *ptr;
    ptr = (int *)malloc(7 * sizeof(int)); // Allocates memory for 7 integers

    // Check if memory allocation was successful
    if (ptr == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    } else {
        printf("Memory allocated successfully.\n");
        free(ptr); // Free allocated memory
    }

    return 0;
}

Output:

Memory allocated successfully.

2calloc(size_t num, size_t size):

Allocates memory for an array of elements, initializes all bytes to zero, and returns a pointer to the beginning of the block.

Syntax:

 void *calloc(size_t num, size_t size);
Example:

// program for calloc in C
#include<stdio.h>
#include<stdlib.h> 

int main() {
    int *ptr;
    ptr = (int *)calloc(5, sizeof(int)); // Allocates memory for 5 integers

    // Check if memory allocation was successful
    if (ptr == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    } else {
        printf("Memory allocated successfully.\n");
        free(ptr); // Free allocated memory
    }

    return 0;
}

Output:

Memory allocated successfully.

3realloc(void *ptr, size_t size):

Resizes the memory block pointed to by ptr to the specified size.

If ptr is NULL, it behaves like malloc.

Syntax:

 void *realloc(void *ptr, size_t size);
Example:

// Program for realloc in C
#include<stdio.h> 
#include<stdlib.h>

int main() {
    int *ptr;
    ptr = (int *)malloc(7 * sizeof(int)); // Allocates memory for 7 integers

    // Resize memory block to accommodate 10 integers
    ptr = (int *)realloc(ptr, 10 * sizeof(int));

    // Check if memory reallocation was successful
    if (ptr == NULL) {
        printf("Memory reallocation failed.\n");
        exit(1);
    } else {
        printf("Memory reallocated successfully.\n");
        free(ptr); // Free allocated memory
    }

    return 0;
}

Output:

Memory reallocated successfully.

Random Number Generation Functions

1rand():

Generates a pseudo-random integer between 0 and RAND_MAX.

Syntax:

int rand(void);
Example:

// program for rand in C
#include<stdio.h>
#include<stdlib.h> 
#include<time.h>

int main() {
    srand(time(0)); // Seed the random number generator

    for (int i = 0; i < 5; i++) {
        printf("%d ", rand());
    }

    return 0;
}

Output:

258730021 2103890938 1154162713 682021446 1113025130

2srand() function:

The srand() function sets the starting point for generating random numbers, ensuring predictability and control over random number sequences.

Syntax:

void srand(unsigned int seed);

Example:

// program for srand() in C
#include<stdio.h>
#include<stdlib.h>

int main() {
    srand(123); // Seed the random number generator with 123

    for (int i = 0; i < 5; i++) {
        printf("%d ", rand());
    }

    return 0;
}

Output:

128959393 1692901013 436085873 748533630 776550279 


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments