Useful functions provided with C++

C++ includes a number of useful functions which you can use in your programs by simply including more #include lines at the top. This page lists a selection of these.

stdlib.h
math.h
ctype.h

stdlib.h

This holds miscellaneous useful functions.

int rand()
returns a random (ok, pseudorandom) integer.
int srand(int seed)
initializes the pseudorandom generator using the value of seed (which should be a reasonably random number). If you use the same seed every time you run the program, you may get the same ``random'' sequence every time you run the program. Often programmers use the return value of time(NULL) as the seed.
int abs(int n)
returns the absolute value of the integer n.

math.h

This holds many of the more useful mathematical functions.

double sin(double x)
returns the sine of x (where x is in radians).
double cos(double x)
returns the cosine of x (where x is in radians).
double tan(double x)
returns the tangent of x (where x is in radians).
double asin(double x)
returns the arcsine of x in radians between -pi/2 and pi/2, for x between -1 and 1.
double acos(double x)
returns the arccosine of x in radians between 0 and pi, for x between -1 and 1.
double atan(double x)
returns the arctangent of x in radians between -pi/2 and pi/2.
double atan2(double y, double x)
returns the angle (in radians between -pi and pi) of a line with rise y for each run of x.
double sinh(double x)
returns the hyperbolic sine of x.
double cosh(double x)
returns the hyperbolic cosine of x.
double tanh(double x)
returns the hyperbolic tangent of x.
double exp(double x)
returns the ex (the exponential function, with e around 2.71828).
double log(double x)
returns the natural logarithm of x.
double log10(double x)
returns the base-10 logarithm of x.
double pow(double x, double y)
returns xy.
double sqrt(double x)
returns the square root of x.
double ceil(double x)
returns x rounded up to the next integer (the smallest integer that is at least x).
double floor(double x)
returns x rounded down to an integer (the largest integer that is at most x).
double fabs(double x)
returns the absolute value for x.
double fmod(double x, double y)
returns a number r between -y and y so that x = i*y + r for some integer i.

ctype.h

This holds functions for working with characters.

int isalnum(char c)
returns 1 if c is a letter or a digit, 0 otherwise.
int isalpha(char c)
returns 1 if c is a letter, 0 otherwise.
int isdigit(char c)
returns 1 if c is a digit, 0 otherwise.
int islower(char c)
returns 1 if c is a lower-case letter, 0 otherwise.
int isupper(char c)
returns 1 if c is a upper-case letter, 0 otherwise.
int isspace(char c)
returns 1 if c is a blank-space character (space, tab, end-of-line), 0 otherwise.
int isupper(char c)
returns 1 if c is a upper-case letter, 0 otherwise.
int isxdigit(char c)
returns 1 if c is a hexadecimal digit, 0 otherwise.
char tolower(char c)
returns the lower-case equivalent of c (which will be c itself if c is not an upper-case letter).
char toupper(char c)
returns the upper-case equivalent of c (which will be c itself if c is not an lower-case letter).