Javatpoint Logo
Javatpoint Logo

cstdlib in C++

In C++, what is cstdlib?

cstdlib in C++

The C++ Standard Library header file (cstdlib in C++) is the header that contains one of the language's most extensively used libraries. This header specifies a set of methods and macros to help teams and technologies write efficient, high-performing, standardised C++ code.

cstdlib in C++

C++ is a widely used programming language, and among the primary reasons for its success was compatible with C, which was and still is a famous and very well language at the time. This flexibility not only made it simpler for coders to adapt, but it also allowed C++ developers to take advantage of pre-existing C code.

Rather than starting from scratch, coders were capable of reused mature code blocks throughout a relatively quick move to C++. They were able to utilise the C Standard library header, stdlib.h, in particular. Nowadays, cstdlib in C++ is an improved C++ version of the initial <stdlib.h>.

cstdlib vs stdlib.h

The C Standard Library Header, <stdlib.h>, provides dependable and effective operations for dynamic allocation of memory, datatype converting, pseudo-random number generating, process control, searching and sorting, math, and multibyte or wide characters to C programmers. Aside from these simple techniques, there are often used constants to improve code consistency across industries and technologies.

Namespaces and Headers

The original C++ specification, C++98, specified that utilising a <c -name-> header was the proper way to use functions inherited from the C library. In a standard C language, for example, one may use "string.h," whereas a similar project in C++ would include <cstring>. In addition, with the introduction of namespaces, newly written C++ library functions were no longer to be declared in the global, undefined namespace. They would instead be defined only in the standard namespace, std.

In the long - term, using cstdlib for C++ indicates that all of the code in <stdlib.h> is declared in the std namespace. As a result, when programmers desire to utilize Standard Library functions, they must be qualified. This can be done explicitly or through the using directive, as shown below:

cstdlib in C++

Figure 1 illustrates the use of std::shared_ptr being used as component of a doubly linked list.

The undefined usage of shared ptr<> in the left column results in an error since the statement does not belong to the global namespace. The namespace is clearly specified in the middle column, indicating to where the compiler may locate it. The using namespace std command in the rightmost column instructs the compiler to look in std for procedures that do not present in the local or global domains. It should be emphasised that the third (rightmost) alternative is regarded as unethical. Another way not mentioned before is to use the std::shared_ptr directive to add merely shared_ptr.

Extra features

The C++ library's cstdlib provides a subset of classic C functions, macros, and datatypes. The declared set of absolute value (abs) functions is an instance of this. C specifies the procedures necessary for determining the absolute values (abs) of an integer, long, or long long value in stdlib.h. There's really, though, no way of knowing the absolute value of a float, double, or long double. These types are instead declared in math.h, another commonly utilized C header.

Absolute value datatype Function C header
Integer int abs(int x) stdlib.h
Long integer long labs(long x) stdlib.h
Long long integer long long llabs(long long x) stdlib.h
Float float fabsf(float x) math.h
Double double fabs(double x) math.h
Long double long double fabsl(long double x) math.h

Figure 2: C Standard Library absolute value functions

All of the following instances are covered by the corresponding abs() overrides in the C++ header's cstdlib.

What exactly is included?

C++ header cstdlib contains various member functions, datatypes, and constant values. The functions defined by the header file are described in the tables below.

Conversion functions

Conversion Functions Explanation
atol Works by converting a string to a long value.
atof Create a double from a string. Remember that the return value is NOT a float.
atoi This function converts a string to an integer.

The functions listed below become more reliable approaches to those listed above.

Conversion Functions Explanation
stltoul This function converts a string to an unsigned long integer.
strtod This function converts a string to a double.
strtoull This function converts a string to an unsigned long long integer.
strtol This function converts a string to a long integer.
strtoll This function converts a string to a long long integer.

Functions relating to random numbers

Random numbers Explanation
random A function that returns an integer in C that is not standard (provided by POSIX)
srandom The seed of the random number generator is set (non-standard C, POSIX)
rand This function generates a pseudo-random integer.
srand Sets the seed value for the random number generator.

Dynamic memory allocation functions

Memory Allocation Explanation
calloc Use the heap to allocate memory (specify size and count; initialise memory)
free Memory should be de-allocated.
malloc Use the heap to allocate memory (specify entire block size)
realloc Modify the amount of memory that has previously been allocated.

Searching and sorting functions

Searching and Sorting Explanation
qsort Quick Sort can be used to sort an array.
bsearch Implement a binary search on an array.

Mathematical functions

Math Functions Explanation
div Divide an integer by its quotient and remainder to get the result.
abs Determine the absolute value of an integer.
ldiv Long integer division with quotient and remainder as output
labs Find the absolute value of a long integer.

Perform with multibyte and wide characters with these functions.

Multibyte / Wide Character Functions Explanation
wctomb Converting a multibyte character to a wide character.
mblen The size of a multibyte character is returned.
mbstowcs Converting a multibyte character sequence to a wide character sequence.
mbtowc Converting a multibyte character to a wide character.
wcstombs Converting a wide character sequence to a multibyte character sequence.

Macros and Constants

The C++ library's cstdlib contains a number of macros and constants that aid in the standardisation of C++ development and code bases. As an instance, consider the constant values returned by the main function:


The EXIT_SUCCESS constant is capable of serving as the main function return value that the requesting framework interprets as successful execution. Even though the number zero also indicates a perfect program execution, EXIT_SUCCESS is implementation-specific.


The EXIT_FAILURE constant also serves as the main function's return value. It does, however, convey to the caller framework that the implementation failed, maybe due to a major operating-system fault.

cstdlib in C++

NULL, which specifies a null pointer constant, RAND MAX, which corresponds to the highest allowable value generated by the rand command, and MB CUR MAX, which is the maximum amount of bytes in a multibyte character for the current locale are other instances of constant values defined by cstdlib in C++.


The header of the C++ general-purpose standard library, abbreviated as cstdlib in C++, provides a basic collection of operations used for data type conversion, pseudo-random number generating, memory allocation, searching, sorting, mathematics, and dealing with wide or multibyte characters. It also includes several handy macros in the type of fixed values. C++ coders frequently utilise types, methods, or constants from cstdlib without needing to include this header because it is included by other headers they can use. Not knowing where particular types and utilities come from can lead to problematic compilation errors later on, when an earlier included header is removed and an observable is shockingly not recognised. Understanding that cstlib may be necessary helps minimize time in such situations.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA