Standard Library Functions in C++
The standard function library is divided into the following categories −
• I/O
• String and character handling
• Mathematical
• Time, date, and localization
• Dynamic allocation
• Miscellaneous
• Wide-character functions
<cstring> header file functions
strlen()
The strlen() function in C++ returns the length of the given C-string. It is defined in the
cstring header file.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
// initialize C-string
char song[] = "We Will Rock You!";
// print the length of the song string
cout << strlen(song);
return 0;
}
Output:
17
strcmp()
The strcmp() function in C++ compares two null-terminating strings (C-strings). The
comparison is done lexicographically. It is defined in the cstring header file.
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[] = "Mega";
char str2[] = "Meta";
// compare str1 and str2 lexicographically
int result = strcmp(str1, str2);
cout << result;
return 0;
}
Output:
-13
strcmp() Return Value
The strcmp() function returns:
• a positive value if the first differing character in lhs is greater than the corresponding
character in rhs.
• a negative value if the first differing character in lhs is less than the corresponding
character in rhs.
• 0 if lhs and rhs are equal.
strcat()
The strcat() function in C++ appends a copy of a string to the end of another string.
The strcat() function takes two arguments: dest and src. This function appends a copy of the
character string pointed to by src to the end of string pointed to by dest. The null terminating
character at the end of dest is replaced by the first character of src and the resulting
character is also null terminated.
The behaviour is undefined if
• the strings overlap.
• the dest array is not large enough to append the contents of src.
• It is defined in <cstring> header file.
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char dest[50] = "Learning C++ is fun";
char src[50] = " and easy";
strcat(dest, src);
cout << dest ;
return 0;
OUTPUT
Learning C++ is fun and easy
strcpy()
The strcpy() function in C++ copies a character string from source to destination. It is
defined in the cstring header file.
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char src[] = "Hello Programmers.";
// large enough to store content of src
char dest[20];
// copy the contents of src to dest
strcpy(dest,src);
cout << dest;
return 0;
}
Output:
Hello Programmers.
strcpy() Parameters
The strcpy() function takes the following parameters:
dest - pointer to a C-string where the contents are copied to
src - pointer to a C-string where the contents are copied from
strcpy() Undefined Behavior
The behaviour of strcpy() is undefined if:
• The memory allocated for dest pointer is not large enough.
• The strings overlap.
<math.h> header file functions:
log()
The log() function in C++ returns the natural logarithm (base-e logarithm) of the argument.
This function is defined in <cmath> header file.
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double x = 13.056, result;
result = log (x);
cout << "log(x) = " << result << endl;
x = -3.591;
result = log (x);
cout << "log(x) = " << result << endl;
return 0;
}
OUTPUT
log(x) = 2.56925
log(x) = nan
log10()
The log10() function in C++ returns the common logarithm (base 10 logarithm) of the
argument.
This function is defined in <cmath> header file.
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
double x = 13.056, result;
result = log10(x);
cout << "log10(x) = " << result << endl;
x = -3.591;
result = log10(x);
cout << "log10(x) = " << result << endl;
return 0;
}
OUTPUT
log10(x) = 1.11581
log10(x) = nan
pow()
The pow() function returns the result of the first argument raised to the power of the second
argument. This function is defined in the cmath header file.
In C++, pow(a, b) = ab.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// computes 5 raised to the power 3
cout << pow(5, 3);
return 0;
}
Output:
125
pow() Parameters
The pow() function takes two parameters:
base - the base value
exponent - exponent of the base
sqrt()
The sqrt() function in C++ returns the square root of a number. This function is defined in
the cmath header file.
Mathematically, sqrt(x) = √x.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Square root of 25 = ";
// print the square root of 25
cout << sqrt(25);
return 0;
}
Output:
Square root of 25 = 5
sqrt() Parameters
The sqrt() function takes the following parameter:
num - a non-negative number whose square root is to be computed
Note: If a negative argument is passed to sqrt(), domain error occurs.
sin()
The sin() function in C++ returns the sine of an angle (argument) given in radians.
This function is defined in <cmath> header file.
sin() Parameters
The sin() function takes a single mandatory argument in radians.
sin() Return value
The sin() function returns the value in the range of [-1, 1]. The returned value is either in
double, float, or long double.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 0.439203, result;
result = sin(x);
cout << "sin(x) = " << result << endl;
double xDegrees = 90.0;
// converting degrees to radians
x = xDegrees*3.14159/180;
result = sin(x);
cout << "sin(x) = " << result << endl;
return 0;
}
OUTPUT
sin(x) = 0.425218
sin(x) = 1
cos()
The cos() function in C++ returns the cosine of an angle (argument) given in radian.
This function is defined in <cmath> header file.
cos() Parameters
The cos() function takes a single mandatory argument in radians.
cos() Return value
The cos() function returns the value in the range of [-1, 1]. The returned value is either in
double, float, or long double.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 0.5, result;
result = cos(x);
cout << "cos(x) = " << result << endl;
double xDegrees = 25;
// converting degrees to radians
x = xDegrees*3.14159/180;
result = cos(x);
cout << "cos(x) = " << result << endl;
return 0;
}
OUTPUT
cos(x) = 0.877583
cos(x) = 0.906308
abs()
The abs() function in C++ returns the absolute value of the argument. It is defined in the
cmath header file.
Mathematically, abs(num) = |num|.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// get absolute value of -5.5
cout << abs(-5.5);
return 0;
}
Output:
5.5
abs() Parameters
The abs() function takes the following parameter:
num - a floating point number whose absolute value is returned. It can be of the following
types:
• double
• float
• long double
The abs() function returns:
the absolute value of num i.e. |num|
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num = -87.91, result;
result = abs(num);
cout << "abs(" << num << ") = |" << num << "| = " << result;
return 0;
}
OUTPUT
abs(-87.91) = |-87.91| = 87.91
<ctype.h> header file functions:
isalnum()
The isalnum() function checks whether the argument passed is an alphanumeric character
(alphabet or number) or not.
The function definition of isalnum() is:
isalnum() Parameters
argument - a character
isalnum() Return Value
Returns 1 if argument is an alphanumeric character.
Returns 0 if argument is neither an alphabet nor a digit.
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char c;
cout<<"enter a character :";
cin>>c;
if (isalnum(c) == 0)
cout<<"entered value is not an alphanumeric character.";
else
cout<<"entered value is an alphanumeric character.";
return 0;
}
OUTPUT
Enter a character: 0
0 is an alphanumeric character.
isdigit()
The isdigit() function in C++ checks if the given character is a digit or not. It is defined in the
cctype header file.
#include <iostream>
using namespace std;
int main() {
// checks if '9' is a digit
cout << isdigit('9');
return 0;
}
Output:
1
isdigit() Parameters
The isdigit() function takes the following parameters:
ch - the character to check, casted to int type or EOF
isdigit() Return Value
The isdigit() function returns:
• a non-zero integer value (true) if ch is a digit
• the integer zero (false) if ch is not a digit
In C++, isupper() and islower() are predefined functions used for string and character
handling. cstring.h is the header file required for string functions and cctype.h is the
headerfile required for character functions.
isupper()
This function is used to check if the argument contains any uppercase letters such as A, B,
C, D, …, Z.
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char x;
cin >> x;
if (isupper(x))
cout << "Uppercase";
else
cout << "Not uppercase.";
return 0;
}
OUTPUT
Input : A
Output : Uppercase
Input : a
Output : Not uppercase
islower()
This function is used to check if the argument contains lowercase letters such as a, b, c, d,
…, z.
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char x;
cin >> x;
if (islower(x))
cout << "Lowercase";
else
cout << "Not Lowercase.";
return 0;
}
OUTPUT
Input:A
Output : Not Lowercase
Input : a
Output : Lowercase
tolower()
The tolower() function in C++ converts a given character to lowercase. It is defined in the
cctype header file.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// convert 'A' to lowercase
char ch = tolower('A');
cout << ch;
return 0;
}
Output:
a
tolower() Parameters
The tolower() function accepts the following parameter:
ch - a character, casted to int type or EOF
tolower() Return Value
The tolower() function returns:
For Alphabets - the ASCII code of the lowercase version of ch
For Non-Alphabets - the ASCII code of ch
toupper()
The toupper() function in C++ converts a given character to uppercase. It is defined in the
cctype header file.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// convert 'a' to uppercase
char ch = toupper('a');
cout << ch;
return 0;
}
Output:
A
toupper() Parameters
The toupper() function takes the following parameter:
ch - a character, casted to int type or EOF
toupper() Return Value
The toupper() function returns:
For Alphabets - the ASCII code of the uppercase version of ch
For Non-Alphabets - the ASCII code of ch
isalpha()
The isalpha() function in C++ checks if the given character is an alphabet or not. It is
defined in the cctype header file.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
// check if '27' is an alphabet
int result = isalpha(27);
cout << result;
return 0;
}
Output:
0
isalpha() Parameters
The isalpha() function takes the following parameter:
ch - the character to check, casted to int or EOF
isalpha() Return Value
The isalpha() function returns:
non-zero value if ch is an alphabet
zero if ch is not an alphabet
isspace()
The isspace() function in C++ checks if the given character is a whitespace character or not.
isspace() Return value
The isspace() function returns non zero value if ch is a whitespace character, otherwise
returns zero.
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "A\tB\nC++";
cout << "Before removing whitespace characters" << endl;
cout << str << endl << endl;
cout << "After removing whitespace characters" << endl;
for (int i=0; i<strlen(str); i++)
{
if (!isspace(str[i]))
cout << str[i];
}
return 0;
}
OUTPUT
Before removing whitespace characters
A B
C++
After removing whitespace characters
ABC++
<stdio.h> header file functions
gets()
The gets() function in C++ reads characters from stdin and stores them until a newline
character is found or end of file occurs.
The gets() function reads characters from stdin and stores them in str until a newline
character or end of file is found.
gets() Return value
On success, the gets() function returns str
On failure, it returns NULL.
If the failure is caused due to end of file condition, it sets the eof indicator on stdin.
If the failure is caused due to some other error, it sets the error indicator on stdin.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char str[100];
cout << "Enter a string: ";
gets(str);
cout << "You entered: " << str;
return 0;
}
OUTPUT
Enter a string: Have a great day!
You entered: Have a great day!
puts()
The puts() function in C++ writes a string to stdout.
The puts() function takes a null terminated string str as its argument and writes it to stdout.
The terminating null character '\0' is not written but it adds a newline character '\n' after
writing the string.
puts() Return value
On success, the puts() function returns a non-negative integer. On failure it returns EOF and
sets the error indicator on stdout.
#include <iostream>
#include <cstdio>
int main()
{
char str1[] = "Happy New Year";
char str2[] = "Happy Birthday";
puts(str1);
/* Printed on new line since '/n' is added */
puts(str2);
return 0;
}
OUTPUT
Happy New Year
Happy Birthday
getchar()
The getchar() function in C++ reads the next character from stdin.
The getchar() function is equivalent to a call to getc(stdin). It reads the next character from
stdin which is usually the keyboard.
It is defined in <cstdio> header file.
getchar() Return value
On success, the getchar() function returns the entered character.
On failure, it returns EOF.
If the failure is caused due to end of file condition, it sets the eof indicator on stdin.
If the failure is caused by some other error, it sets the error indicator on stdin.
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int c,i=0;
char str[100];
cout << "Enter characters, Press Enter to stop\n";
do
{
c = getchar();
str[i] = c;
i++;
} while(c!='\n');
cout << str;
return 0;
}
putchar()
The putchar() function in C++ writes a character to stdout.
The putchar() function takes an integer argument to write it to stdout. The integer is
converted to unsigned char and written to the file. A call to putchar(ch) is equivalent to
putc(ch, stdout).
It is defined in <cstdio> header file.
#include <iostream>
#include <cstdio>
int main()
{
for (int i=48; i<58; i++)
{
/* Writes the equivalent character */
putchar(i);
putchar(' ');
}
return 0;
}
<conio.h> header file functions:
Clrscr()
It is a predefined function in "conio.h" (console input output header file) used to clear the
console screen. It is a predefined function, by using this function we can clear the data from
console (Monitor). Using of clrscr() is always optional but it should be place after variable or
function declaration only.
Getch()
It is a predefined function in "conio.h" (console input output header file) will tell to the
console wait for some time until a key is hit given after running of program.
By using this function we can read a character directly from the keyboard. Generally getch()
are placing at end of the program after printing the output on screen.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=10, b=20;
int sum=0;
sum=a+b;
cout<<"Sum: "<<sum;
getch(); // use getch() befor end of main()
return 0;
OUTPUT:
Sum: 30