C Standard Library -<stdlib.h>
Introduction
stdlib .hThe header file defines four variable types, some macros, and various general utility functions.
<stdlib.h>is a header file in the C standard library that provides many general-purpose utility functions, including memory allocation, process control, sorting and searching, and string conversion, among others.
Library variables
The following are the variable types defined in the header file stdlib.h:
| Serial Number | Variables & Description |
|---|---|
| 1 | size_t This is an unsigned integer type, which issizeofResult of the keyword. |
| 2 | wchar_t This is an integer type for the size of a wide character constant. |
| 3 | div_t This isdivThe structure returned by the function. |
| 4 | ldiv_t This isldivThe structure returned by the function. |
Library macros
The following are the macros defined in the header file stdlib.h:
| Serial Number | Macros & Description |
|---|---|
| 1 | NULL This macro is the value of a null pointer constant. |
| 2 | EXIT_FAILURE This is the value to be returned when the exit function fails. |
| 3 | EXIT_SUCCESS This is the value to be returned when the exit function succeeds. |
| 4 | RAND_MAX This macro is the maximum value returned by the rand function. |
| 5 | MB_CUR_MAX This macro represents the maximum number of characters in a multibyte character set, which cannot be greater than MB_LEN_MAX. |
Library functions
The following are the functions defined in the header file stdlib.h:
| Serial Number | Functions & Description |
|---|---|
| 1 | double atof(const char *str) Pass the parameterstrConverts the string pointed to into a floating-point number (of type double). |
| 2 | int atoi(const char *str) Pass the parameterstrConverts the string pointed to into an integer (of type int). |
| 3 | long int atol(const char *str) Pass the parameterstrConverts the string pointed to into a long integer (of type long int). |
| 4 | double strtod(const char *str, char **endptr) Pass the parameterstrConverts the string pointed to into a floating-point number (of type double). |
| 5 | long int strtol(const char *str, char **endptr, int base) Pass the parameterstrConverts the string pointed to into a long integer (of type long int). |
| 6 | unsigned long int strtoul(const char *str, char **endptr, int base) Pass the parameterstrConverts the string pointed to into an unsigned long integer (of type unsigned long int). |
| 7 | void *calloc(size_t nitems, size_t size) Allocates the required memory space and returns a pointer to it. |
| 8 | void free(void *ptr) Call before releasingcalloc、mallocorreallocThe allocated memory space. |
| 9 | void *malloc(size_t size) Allocates the required memory space and returns a pointer to it. |
| 10 | void *realloc(void *ptr, size_t size) Call before attempting to resizemallocorcallocThe size of the allocated memory block pointed to by ptr. |
| 11 | void abort(void) Causes an abnormal program termination. |
| 12 | int atexit(void (*func)(void)) Calls the specified function when the program terminates normally.func。 |
| 13 | void exit(int status) Causes the program to terminate normally. |
| 14 | char *getenv(const char *name) Searches for the environment string pointed to by name and returns the associated value to the string. |
| 15 | int system(const char *string) The command specified by string is passed to the host environment for execution by the command processor. |
| 16 | void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *)) Perform a binary search. |
| 17 | void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) Sort the array. |
| 18 | int abs(int x) Returns the absolute value of x. |
| 19 | div_t div(int numer, int denom) Divide the numerator by the denominator. |
| 20 | long int labs(long int x) Returns the absolute value of x. |
| 21 | ldiv_t ldiv(long int numer, long int denom) Divide the numerator by the denominator. |
| 22 | int rand(void) Returns a range from 0 toRAND_MAXa pseudo-random number between. |
| 23 | void srand(unsigned int seed) This function seeds the random number generator used by the functionrandthe random number generator used. |
| 24 | int mblen(const char *str, size_t n) Return parameterstrThe length of the multibyte character pointed to. |
| 25 | size_t mbstowcs(schar_t *pwcs, const char *str, size_t n) Pass the parameterstrThe multibyte character string pointed to is converted into parameters.pwcsThe array it points to. |
| 26 | int mbtowc(wchar_t *pwc, const char *str, size_t n) Check parametersstrThe multibyte character pointed to. |
| 27 | size_t wcstombs(char *str, const wchar_t *pwcs, size_t n) Put the arraypwcsThe encoding stored therein is converted into multibyte characters and stored in the string.strIn. |
| 28 | int wctomb(char *str, wchar_t wchar) Checks against the parameterwcharThe encoding of the multibyte character given. |
Example
The following is using<stdlib.h>Examples of some functions in.
Dynamic memory allocation:
Example
#include <stdlib.h>
int main() {
int *array = (int*)malloc(10 * sizeof(int));
if (array == NULL) {
perror("Memory allocation failed");
return 1;
}
for (int i = 0; i < 10; i++) {
array[i] = i;
printf("%d ", array[i]);
}
printf("\n");
free(array);
return 0;
}
Pseudo-random number generation:
Example
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
for (int i = 0; i < 10; i++) {
printf("%d ", rand() % 100);
}
printf("\n");
return 0;
}
String conversion:
Example
#include <stdlib.h>
int main() {
const char *str = "12345";
int num = atoi(str);
printf("The integer value is %d\n", num);
return 0;
}
Quick sort:
Example
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int array[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(array) / sizeof(array[0]);
qsort(array, size, sizeof(int), compare);
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
Notes
- Usage
malloc、callocandreallocThe allocated memory must be freed usingfreeto avoid memory leaks. atoi、atolandatofThe function does not provide error checking; ensure the input format is correct when using it. It is recommended to usestrtol、strtoulandstrtodto perform safer conversions.qsortandbsearchThe function requires a custom comparison function to handle different data types and sorting requirements.
By understanding and using<stdlib.h>The functions provided allow operations such as memory management, process control, random number generation, environment management, mathematical conversions, and sorting and searching in C programs, thereby enabling the writing of more flexible and feature-rich programs.