The C++ standard library
is a header file in the C++ standard library that provides various general-purpose utility functions, including memory allocation, process control, environment queries, sorting and searching, numeric conversion, pseudo-random number generation, etc. These functions originally come from the C standard library , and have been standardized and extended in C++.
Syntax
In a C++ program, to use cstdlib 's functions, you first need to include this header:
#include
Common functions
cstdlib contains many useful functions; here are some commonly used ones with brief descriptions:
exit(int status): Terminates program execution and returns a status code.system(const char* command): Executes a command-line string.malloc(size_t size): Allocates a block of memory of the specified size.free(void* ptr): Frees previously allocated memory.atoi(const char* str): Converts a string to an integer.atof(const char* str): Converts a string to a floating-point number.rand(): Generates a random number.srand(unsigned int seed): Sets the seed for the random number generator.
Example
The following are some uses of cstdlib 's functions:
Example 1: Using exit function
Example
#include
#include
int
main
(
)
{
std
::
cout
<<
"This program will exit now."
<<
std
::
endl
;
exit
(
0
)
;
// Exit the program normally
return
0
;
// This line of code will not be executed
}
Output result:
This program will exit now.
Example 2: Using system function
Example
#include
#include
int
main
(
)
{
std
::
cout
<<
"Executing a system command: dir"
<<
std
::
endl
;
system
(
"dir"
)
;
// Display files and folders in the current directory on Windows
return
0
;
}
Output result:
Executing a system command: dir
then displays the list of files and folders in the current directory.
Example 3: Using malloc and free function
Example
#include
#include
int
main
(
)
{
int
ptr
=
(
int
)
malloc
(
10
sizeof
(
int
)
)
;
// Allocate memory
if
(
ptr
==
NULL
)
{
std
::
cout
<<
"Memory allocation failed."
<<
std
::
endl
;
return
1
;
}
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
ptr
[
i
]
=
i
i
;
// Use the allocated memory
}
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
std
::
cout
<<
"Element "
<<
i
<<
": "
<<
ptr
[
i
]
<<
std
::
endl
;
}
free
(
ptr
)
;
// Free memory
return
0
;
}
Output result:
Element 0: 0
Element 1: 1
Element 2: 4
...
Element 9: 81
Example 4: Using atoi and atof function
Example
#include
#include
int
main
(
)
{
std
::
string
str1
=
"123"
;
std
::
string
str2
=
"456.78"
;
int
num1
=
std
::
atoi
(
str1.
c_str
(
)
)
;
// Convert string to integer
double
num2
=
std
::
atof
(
str2.
c_str
(
)
)
;
// Convert string to floating-point
std
::
cout
<<
"Integer: "
<<
num1
<<
std
::
endl
;
std
::
cout
<<
"Float: "
<<
num2
<<
std
::
endl
;
return
0
;
}
Output result:
Integer: 123
Float: 456.78
Example 5: Using rand and srand function
Example
#include
#include
#include
int
main
(
)
{
std
::
srand
(
std
::
time
(
nullptr
)
)
;
// Use current time as the random seed
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
std
::
cout
<<
std
::
rand
(
)
%
100
<<
" "
;
// Generate a random number between 0 and 99
}
std
::
cout
<<
std
::
endl
;
return
0
;
}
Output result:
53 22 62 68 62