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 NumberVariables & Description
1size_t
This is an unsigned integer type, which issizeofResult of the keyword.
2wchar_t
This is an integer type for the size of a wide character constant.
3div_t
This isdivThe structure returned by the function.
4ldiv_t
This isldivThe structure returned by the function.

Library macros

The following are the macros defined in the header file stdlib.h:

Serial NumberMacros & Description
1NULL
This macro is the value of a null pointer constant.
2EXIT_FAILURE
This is the value to be returned when the exit function fails.
3EXIT_SUCCESS
This is the value to be returned when the exit function succeeds.
4RAND_MAX
This macro is the maximum value returned by the rand function.
5MB_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 NumberFunctions & Description
1double atof(const char *str)
Pass the parameterstrConverts the string pointed to into a floating-point number (of type double).
2int atoi(const char *str)
Pass the parameterstrConverts the string pointed to into an integer (of type int).
3long int atol(const char *str)
Pass the parameterstrConverts the string pointed to into a long integer (of type long int).
4double strtod(const char *str, char **endptr)
Pass the parameterstrConverts the string pointed to into a floating-point number (of type double).
5long int strtol(const char *str, char **endptr, int base)
Pass the parameterstrConverts the string pointed to into a long integer (of type long int).
6unsigned 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).
7void *calloc(size_t nitems, size_t size)
Allocates the required memory space and returns a pointer to it.
8void free(void *ptr)
Call before releasingcalloc、mallocorreallocThe allocated memory space.
9void *malloc(size_t size)
Allocates the required memory space and returns a pointer to it.
10void *realloc(void *ptr, size_t size)
Call before attempting to resizemallocorcallocThe size of the allocated memory block pointed to by ptr.
11void abort(void)
Causes an abnormal program termination.
12int atexit(void (*func)(void))
Calls the specified function when the program terminates normally.func。
13void exit(int status)
Causes the program to terminate normally.
14char *getenv(const char *name)
Searches for the environment string pointed to by name and returns the associated value to the string.
15int system(const char *string)
The command specified by string is passed to the host environment for execution by the command processor.
16void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
Perform a binary search.
17void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
Sort the array.
18int abs(int x)
Returns the absolute value of x.
19div_t div(int numer, int denom)
Divide the numerator by the denominator.
20long int labs(long int x)
Returns the absolute value of x.
21ldiv_t ldiv(long int numer, long int denom)
Divide the numerator by the denominator.
22int rand(void)
Returns a range from 0 toRAND_MAXa pseudo-random number between.
23void srand(unsigned int seed)
This function seeds the random number generator used by the functionrandthe random number generator used.
24int mblen(const char *str, size_t n)
Return parameterstrThe length of the multibyte character pointed to.
25size_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.
26int mbtowc(wchar_t *pwc, const char *str, size_t n)
Check parametersstrThe multibyte character pointed to.
27size_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.
28int 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 <stdio.h>
#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 <stdio.h>
#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 <stdio.h>
#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 <stdio.h>
#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

  • Usagemalloc、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.

other extensions