Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library –

C Standard Library -

Introduction

stdlib .h The header file defines four variable types, some macros, and various general utility functions.

is a header file in the C Standard Library that provides many general utility functions, including memory allocation, process control, sorting and searching, and string conversion.

Library variables

The variable types defined in the header file stdlib.h are listed below:

No. Variable & Description
1 **size_t **
This is an unsigned integer type; it is the sizeof The result of the keyword.
2 **wchar_t **
This is an integer type the size of a wide character constant.
3 **div_t **
This is div The structure returned by the function.
4 **ldiv_t **
This is ldiv The structure returned by the function.

Library macros

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

No. Macro & description
1 NULL
This macro is the value of a null pointer constant.
2 EXIT_FAILURE
This is the value to return when the exit function fails.
3 EXIT_SUCCESS
This is the value to return 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 bytes in a multibyte character, and cannot be greater than MB_LEN_MAX.

Library functions

The functions defined in the header file stdlib.h are listed below:

No. Function & Description
1 double atof(const char *str)
Converts the string pointed to by the argument str into a floating-point number (of type double).
2 int atoi(const char *str)
Converts the string pointed to by the argument str The string pointed to by the argument is converted into an integer (of type int).
3 long int atol(const char *str)
Converts the string pointed to by the argument str The string pointed to by the argument is converted into a long integer (of type long int).
4 double strtod(const char *str, char **endptr)
Converts the string pointed to by the argument str into a floating-point number (of type double).
5 long int strtol(const char *str, char **endptr, int base)
Converts the string pointed to by the argument str The string pointed to by the argument is converted into a long integer (of type long int).
6 unsigned long int strtoul(const char *str, char **endptr, int base)
Converts the string pointed to by the argument str Converts the string pointed to by nptr 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)
Frees the memory space previously allocated by a call to calloc, malloc or realloc malloc, calloc, or realloc.
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)
Attempts to resize the memory block previously allocated by a call to malloc or calloc The size of the memory block pointed to by ptr that was allocated by the allocation function.
11 void abort(void)
Causes abnormal program termination.
12 int atexit(void (*func)(void))
When the program terminates normally, the specified function is called func.
13 void exit(int status)
Causes normal program termination.
14 char *getenv(const char *name)
Search for the environment string pointed to by name and return the associated value as a string.
15 int system(const char *string)
Passes the command specified by string to the host environment to be executed 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 *))
Performs a binary search.
17 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void , const void))
Sorts an array.
18 int abs(int x)
Returns the absolute value of x.
19 div_t div(int numer, int denom)
Divides 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)
Divides the numerator by the denominator.
22 int rand(void)
Returns a pseudo-random number between 0 and RAND_MAX inclusive.
23 void srand(unsigned int seed)
This function seeds the pseudo-random number sequence used by the function rand The random number generator used by the function.
24 int mblen(const char *str, size_t n)
Returns the length of the multibyte character pointed to by str the given parameter.
25 size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)
Converts the string pointed to by the argument str Converts the string of multibyte characters pointed to by the input into the argument pwcs pointed to by the output.
26 int mbtowc(wchar_t *pwc, const char *str, size_t n)
Checks the argument str for valid multibyte characters.
27 size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)
Converts the encoding stored in the array pwcs into multibyte characters and stores them in the string str it.
28 int wctomb(char *str, wchar_t wchar)
Checks the encoding corresponding to the argument wchar for the given multibyte character.

Example

The following is how to use Examples of some functions in stdlib.h.

Dynamic memory allocation:

Example

#include

#include

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

#include

#include

int

main

(

)

{

   

srand

(

time

(

NULL

)

)

;

   

for

(

int

i

=

0

;

i

<

10

;

i

++

)

{

       

printf

(

"%d "

,

rand

(

)

%

100

)

;

   

}

   

printf

(

"\n"

)

;

   

return

0

;

}

String conversion:

Example

#include

#include

int

main

(

)

{

   

const

char

str

=

"12345"

;

   

int

num

=

atoi

(

str

)

;

   

printf

(

"The integer value is %d\n"

,

num

)

;

   

return

0

;

}

Quick sort:

Example

#include

#include

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

By understanding and using The functions provided enable memory management, process control, random number generation, environment management, mathematical conversions, sorting and searching, and more in C programs, allowing you to write more flexible and feature-rich programs.