Library Functions In C++

Get To Know All About Library Functions In C++ With Examples.

Library functions which are also called as “built-in” functions are the functions that are already available and implemented in C++.

We can directly call these functions in our program as per our requirements. Library functions in C++ are declared and defined in special files called “Header Files” which we can reference in our C++ programs using the “include” directive.

=> Visit Here For The Complete C++ Course From Experts.

LIBRARY FUNCTIONS in C++

Overview

For Example, to include all the built-in functions related to math, we should include <cmath> header as follows:

#include <cmath>

Some of the standard library header files that are used in C++ are tabularized as below. These headers replace their respective counterparts with “.h” extension.

For Example, <iostream> replaces <iostream.h> header file.

The Header files are briefed along with their description below.

HeadersDescription
iostreamThis header contains the prototype for standard input and output functions used in C++ like cin, cout, etc.
cmathThis is the header containing various math library functions.
iomanipThis header contains stream manipulator functions that allow us to format the stream of data.
cstdlibThe header cstdlib contains various functions related to conversion between text and numbers, memory allocation, random numbers, and other utility functions.
ctime ctime contains function prototypes related to date and time manipulations in C++.
cctypeThis header includes function prototypes that test the type of characters (digit, punctuation, etc.). It also has prototypes that are used to convert from uppercase to lowercase and another way around.
cstring cstring header includes function prototypes for C-style string-processing functions.
cstdioThis header contains function prototypes for the C-style standard input/output library functions which we included initially in stdio.h
fstreamFunction prototypes for functions that perform input/output from/to files on disk are included in fstream header.
climits climits header has the integral size limits of the system.
cassert cassert header contains macros and variables for adding diagnostics that help us in program debugging.
cfloatThis header file contains the size limits for floating-point numbers on the system.
stringThe header string defines the class string of the C++ Standard Library.
list, vector, stack, queue, deque, map,  set, bitsetAll these headers are used for Standard Template Library (STL) implementation. Each of these headers contains the respective class definition and function prototypes.
typeinfoThis header contains various classes for Runtime Type Identification (RTTI).
exception,  stdexceptAll the classes and functions used for exception handling in C++ are included in these two headers.
memoryThis header is used by the C++ standard library to allocate memory.
sstreamFunctions that read input from strings in memory and output to strings in memory require functions prototypes from sstream header to implement the functionality.
functionalUsed by C++ standard library algorithms.
iteratorFunction prototypes and classes in this header are used by Standard Template Library to traverse through or iterate through the data inside containers.
algorithmMethods that act on STL container data are included in this header algorithm
localeTo process data in the original natural form for different languages or locales (currencies, character presentation, etc.), the locale header definitions are used.
limitsThis header defines the data type limit for Numbers on each platform.
utilityThis header contains utility functions and classes used by the Standard C++ library.

We have already used most of these headers throughout our tutorial so far. Notable is <iostream>, <string>, <ctime> headers that we have used from time to time.

In our STL tutorials, we will be making use of all the container headers and <algorithm> as well as <iterator> header. Similarly, when we learn file I/O and exception handling, we will be using respective headers.

In this tutorial, we will mostly deal with <cmath> and <cctype> headers and discuss the various function prototypes that they support. The function prototypes from these headers are extensively used in C++ programming.

<cmath> Header

This header contains various function prototypes related to mathematical functions. Some of the prototypes that are used extensively are listed here.

FunctionDescription
sqrt(x)Accepts any non-negative numeric parameter x and returns the square root of this number x
pow(base,exponent)Raises the ‘base’ value to the power specified by the exponent. Returns base^exponent.
exp(x)Takes any number (positive, negative or zero) as a parameter and returns exponential (Euler’s number) e raised to the given parameter
fabs(x)Returns absolute value of an argument.
log(x)Returns the natural logarithm (to the base e) of value x
log 10(x)Return the logarithm (to the base 10) of value x
sin(x)Returns sine of the angle x (in radians)
cos(x)Returns cosine of angle x (in radians)
tan(x)Returns tangent of angle x (in radians)
asin(x)Returns inverse sine (in radians) of number x
acos(x)Returns inverse cosine (in radians) of number x
atan(x)Returns inverse tangent (in radians) of number x

<cctype> Header

This header contains function prototypes that are mainly used for converting the character to upper/lower case or to check if a character is a digit etc.

Function prototypes included in <cctype> header are listed as below:

FunctionDescription
toupper(ch)Takes in character ‘ch’ as an argument and returns the uppercase equivalent of ch if it's present otherwise returns ch.
tolower(ch)Takes in character ‘ch’ as an argument and returns the lowercase equivalent of ch if it's present otherwise returns ch.
isalpha(ch)Returns non-zero if ch is alphabet otherwise 0.
isalnum(ch)Returns non-zero if ch is alphanumeric (alphabet or number) otherwise 0.
isupper(ch)Returns non-zero value if ch is uppercase otherwise 0.
isdigit(ch)Returns non-zero value if ch is a number otherwise 0.
islower()Returns non-zero value if ch is lowercase otherwise 0.

<stdlib> Header

We also have another header <stdlib> that includes various useful library functions that are used extensively in C++ programming.

We have listed some of the popular functions in <stdlib> below:

FunctionDescription
abs(x)Returns absolute value of an integral argument x
atof(const char* str)Converts string to double; returns double
atoi(const char* str)Converts string to int; returns an int
atol(const char* str)Converts string to long int; returns a long int
atoll(const char* str)Converts string to long long int; returns a long long int
strtodConverts string to double
strtolConverts string to long int
strtoulConverts string to unsigned long integer
strtofConverts string to float
strtoldConverts string to long double
strtoullConverts string to unsigned long long integer
strtollConverts string to long long integer
srand(int seed)This is a pseudo-random generator that is initialized to argument ‘seed’
qsortSorts elements of the array in ascending order(internally uses quick sort method)
abortAborts the process resulting in abnormal program termination
atexitHas function passed as an argument which is executed resulting in normal program termination.
malloc(size_t size)Used to allocate memory specified by size and return a pointer to it
calloc ( size_t num, size_t size)Allocates memory of (num*size) bytes with all bits initialized to zero
free(void* ptr)Deallocates memory block allocated by malloc, calloc or realloc function call.
realloc ( void* ptr, size_t size )Resizes the memory block pointed to by ptr that was initially allocated using malloc or calloc function call.
quick_exitNormal termination of the process after returning control to the host environment.
systemInvokes command processor to execute system command passed as an argument
getenvRetrieves the value of environment string passed as an argument to the function
wctombConvert the wide character to a multibyte sequence
wcstombsConvert wide character string to multibyte string

Conclusion

In this tutorial, we have gone through some of the header files supported by the C++ standard library.

We also discussed some popular library functions that are used by programmers. This list of functions is not exhaustive as each header of the C++ standard library contains too many functions for the benefit of programmers.

In our upcoming C++ tutorials, we will come across more library functions.

=> Visit Here For The Exclusive C++ Training Tutorial Series.