Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Header Files

C Library – stdlib.h

Posted in C Tutorial
Updated on Jun 02, 2024
By Mari Selvan
👁️ 21 - Views
⏳ 4 mins
💬 0
C Library - stdlib.h

Photo Credit to CodeToFun

🙋 Introduction

The <stdlib.h> header file in the C programming language is a vital part of the standard library, providing a range of functions for performing general utility operations. These include memory allocation, process control, conversions, and other miscellaneous functions. Understanding the capabilities of <stdlib.h> is essential for effective C programming.

💡 Syntax

To use the functions and macros provided by <stdlib.h>, include the header file at the beginning of your C program:

example.c
Copied
Copy To Clipboard
#include <stdlib.h>

📚 Key Functions and Macros

The following are the list of Memory Allocation Functions and macros

  1. malloc():

    Allocates a specified number of bytes and returns a pointer to the allocated memory.

    example.c
    Copied
    Copy To Clipboard
    void *ptr = malloc(size_t size);
  2. calloc():

    Allocates memory for an array of elements, initializes them to zero, and returns a pointer to the memory.

    example.c
    Copied
    Copy To Clipboard
    void *ptr = calloc(size_t num, size_t size);
  3. realloc():

    Resizes previously allocated memory.

    example.c
    Copied
    Copy To Clipboard
    void *ptr = realloc(void *ptr, size_t size);
  4. free():

    Frees previously allocated memory.

    example.c
    Copied
    Copy To Clipboard
    free(void *ptr);

The following are the list of Process Control Functions and macros

  1. exit():

    Terminates the calling process.

    example.c
    Copied
    Copy To Clipboard
    void exit(int status);
  2. abort():

    Abnormally terminates the process.

    example.c
    Copied
    Copy To Clipboard
    void abort(void);

The following are the list of Conversion functions and macros

  1. atoi():

    Converts a string to an integer.

    example.c
    Copied
    Copy To Clipboard
    int num = atoi(const char *str);
  2. atof():

    Converts a string to a floating-point number.

    example.c
    Copied
    Copy To Clipboard
    double num = atof(const char *str);
  3. strtol():

    Converts a string to a long integer.

    example.c
    Copied
    Copy To Clipboard
    long int num = strtol(const char *str, char **endptr, int base);
  4. strtod():

    Converts a string to a double.

    example.c
    Copied
    Copy To Clipboard
    double num = strtod(const char *str, char **endptr);

The following are the list of Miscellaneous Functions and macros

  1. rand():

    Returns a pseudo-random number.

    example.c
    Copied
    Copy To Clipboard
    int random_number = rand(void);
  2. srand():

    Seeds the random number generator used by rand().

    example.c
    Copied
    Copy To Clipboard
    void srand(unsigned int seed);

📝 Basic Example

Here's a complete example demonstrating the use of several <stdlib.h> functions:

example.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <stdlib.h>

int main() {
   // Memory allocation
   int *arr = (int *)malloc(5 * sizeof(int));
   if (arr == NULL) {
      fprintf(stderr, "Memory allocation failed\n");
      exit(1);
   }

   // Initialize array
   for (int i = 0; i < 5; i++) {
      arr[i] = i + 1;
   }

   // Print array elements
   for (int i = 0; i < 5; i++) {
      printf("%d ", arr[i]);
   }
   printf("\n");

   // Free allocated memory
   free(arr);

   // String to integer conversion
   char str[] = "12345";
   int num = atoi(str);
   printf("The integer value is %d\n", num);

   return 0;
}

💻 Output

Output
1 2 3 4 5 
The integer value is 12345

💰 Benefits

Using the <stdlib.h> library offers several benefits:

  • Versatility: Provides a wide range of functions that cover various aspects of programming, from memory management to process control and numerical conversions.
  • Standardization: As part of the C standard library, <stdlib.h> ensures that your code is portable and adheres to standard practices across different platforms and compilers.
  • Efficiency: The functions in <stdlib.h> are optimized for performance, which is crucial for developing efficient and responsive applications.
  • Ease of Use: The straightforward nature of the functions and macros in <stdlib.h> makes it accessible to both beginners and experienced programmers.
  • Robustness: Offers robust utilities for handling common programming tasks, reducing the likelihood of errors and improving code reliability.

🎉 Conclusion

The <stdlib.h> library in C is an indispensable resource for developers, providing essential functions for memory management, process control, conversions, and more.

Mastering the use of <stdlib.h> functions enhances your ability to write efficient, portable, and reliable C programs.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy