C library reference

Introduction

Contemporary programming systems provide a rich set of library functions that make design and coding easier. Use of standard library functions offer several advantages:

Professional programmers draw on libraries whenever they can. Schedules are always tight and why debug "string copy" when there are more fun problems to solve!

C++ programs may use either C++ libraries (such as those dealing with input/output) or C standard libraries. The C standard libraries contain some well-known, well-worn gems like string manipulation, math functions, etc. This tech note is a brief overview of the C standard libraries that will assist development of our class projects. This overview is not exhaustive or complete! Please don't hesitate to explore on your own. Use the UNIX apropos or man command to search for and read about other library functions.

Namespaces

Namespaces are a C++ feature that assist the construction of very large software systems. When a system gets to several thousand lines of code, it's easy to get clashes between the names (variables, functions, etc.) used by different modules in the system. Programmers sometimes use naming conventions to avoid such clashes, but this approach requires personal initiative and discipline, and is not conducive to automatic checking by the compiler. A namespace allows scope and name management.

We will not use namespaces in our C++ programs because the programs will be rather small and clashes will be easy to control. However, you must be aware of one important use of namespaces -- the separation of current standard and pre-standard library functions. To prevent misuse of current standard library functions (by inadvertently accessing pre-standard library functions), current standard library functions are contained in the std namespace. Thus, you will need to add the C++ declaration:

    using namespace std ;
to correctly select the current standard library. To further reduce the likelyhood of error, current standard library names begin with the letter "c" and do not end in ".h".

A few useful libraries

Here is a table of some useful standard C libraries.

cassert Check assumption/correctness by asserting a logical condition
cctype Character functions
cfloat Maximum and minimum values of floating-point types
climits Maximum and minimum values of integer types
cmath Mathematical library functions
cstdlib Standard C library
cstring Standard C string library

The subsections that follow describe many of the functions in these standard C libraries. The descriptions below are terse. Please use the UNIX man command to get more information about a function before using it. Just enter a command like:

    man isalpha
to get more information about a function. In this command, we are asking for more information about the isalpha function. The man page will contain information about restrictions on argument values, return values and error conditions. Be sure to program defensively -- detect and handle error conditions before your program tilts over and dies.

cassert

The assert macro tests the given expression and if it is false, the program is terminated. A diagnostic message is written to the standard error output stream.

assert(expression) Expression verification macro

cctype

The functions in the cctype library operate on individual characters. There are functions for detecting certain character types (e.g., letter, digit) and for converting between upper and lower case.

isalnum(c) Returns true if c is a letter or digit; otherwise, false
isalpha(c) Returns true if c is a letter; otherwise, false
iscntrl(c) Returns true if c is a control character; otherwise, false
isdigit(c) Returns true if c is a decimal digit; otherwise, false
isgraph(c) Returns true if c is printing character (except space); otherwise, false
islower(c) Returns true if c is lower case; otherwise, false
isprint(c) Returns true if c is a printing character; otherwise, false
ispunct(c) Returns true if c is a punctuation character; otherwise, false
isspace(c) Returns true if c is a white space character; otherwise, false
isupper(c) Returns true if c is upper case; otherwise, false
isxdigit(c) Returns true if c is a hexadecimal digit; otherwise, false
tolower(c) Returns lower case equivalent of c
toupper(c) Returns upper case equivalent of c

cstring

The standard C string functions operate on the common, one-dimensional character array representation of strings. In general, these strings must be terminated by a null character, '\0'.

strcat(s1, s2) Append (concatenate) s2 to s1
strncat(s1, s2, n) Append (concatenate) at most n characters of s2 to s1
strcpy(s1, s2) Copy s2 to s1; Note the direction of the copy!
strncpy(s1, s2, n) Copy first n characters of s2 to s1; Note the direction of the copy!
strlen(s) Return the length of string s, not counting the terminating null character.
strcmp(s1, s2) Compare s1 with s2; Return an integer less than zero, equal to zero, or greater than zero if s1 is less than, equal to, or greater than s2
strncmp(s1, s2, n) Like strcmp above, but compare only the first n characters of s1 and s2
strchr(s, c) Return a pointer to the first occurence of character c in string s; return null (zero) if not found
strrchr(s, c) Return a pointer to the last occurence of character c in string s; return null (zero) if not found
strstr(s1, s2) Return a pointer to the first occurence of string s1 in string s2; return null (zero) if not found
strpbrk(s1, s2) Return a pointer to the first occurence in string s1 of any character contained in string s2; return null (zero) if not found (The characters in s2 are sometimes called "break" characters.)
strspn(s1, s2) Span the first part of string s1 using the characters in string s2; That is, return the number of characters in s1 before the occurrence of any character in string s2
strcspn(s1, s2) Span the first part of string s1 using the complement of the set of characters in string s2; That is, return the number of characters in s1 before the occurrence of any character not in string s2

cstdlib

The functions in the cstdlib library are an eclectic and varied lot. It includes functions to convert from a string representation of a number to its internal representation, to take the absolute value of an integr, to generate a pseudo-random integer number, and to explicitly terminate a program, among many others.

int abs(int j) Return the absolute value of integer j.
double atof(s) Convert the external floating-point representation of the string s to the internal numeric representation of its value and return the double value.
int atoi(s) Convert the external integer representation of the string s to the internal numeric representation of its value and return the int value.
long int atof(s) Convert the external integer representation of the string s to the internal numeric representation of its value and return the long int value.
void exit(int status) Terminate program execution; Return exit status to the operating system; By convention, a status value of zero indicates normal termination while non-zero values indicate an error condition; A program may also return EXIT_SUCCESS or EXIT_FAILURE
int rand() Return a pseudo-random integer in the range from 0 to RAND_MAX
void srand(int seed) Sets the seed (initial value) for pseudo-random number generation; A sequence of pseudo-random numbers is repeatable by setting the same seed value

The pseudo-random number generator implemented by the functions above is a rather poor one. It has a short period and the sequence of pseudo-random numbers quickly repeats. A better pseudo-random number generator is implemented by the functions in the table below.

long random(void) Returns a pseudo-random number in the range 1 to 231-1
void srandom(unsigned int seed) Initializes the pseudo-random number generator with seed
char initstate(...) Initializes the pseudo-random number generator in great and gory detail (advanced feature)
char setstate(...) Allows switching between pseudo-random number generator states (advanced feature)

cmath

The standard C math library defines a large number of useful mathematical functions. The table below lists just a few of these functions.

double cos(double x) Return the cosine of x (radians)
double exp(double x) Return ex
double fabs(double x) Return absolute value of x
double log(double x) Return the natural logarithm of x
double log10(double x) Return the base-10 logarithm of x
double sin(double x) Return the sine of x (radians)
double sqrt(double x) Return the square root of x (x greater than or equal to zero)
double tan(double x) Return the tangent of x (radians)

climits

This library defines the following symbolic constants. These constants specify the smallest and largest value that can be held by a particular C/C++ character or integer variable.

SCHAR_MIN Minimum value a signed char can hold
SCHAR_MAX Maximum value signed char can hold
UCHAR_MAX Maximum value unsigned char can hold
CHAR_MIN Minimum value char can hold
CHAR_MAX Maximum value char can hold
SHORT_MIN Minimum value short int can hold
SHORT_MAX Maximum value short int can hold
USHRT_MAX Maximum value unsigned short int can hold
INT_MIN Minimum value int can hold
INT_MAX Maximum value int can hold
UINT_MAX Maximum value unsigned int can hold
LONG_MIN Minimum value long int can hold
LONG_MAX Maximum value long int can hold
ULONG_MAX Maximum value unsigned long can hold

cfloat

This library defines the following symbolic constants. These constants specify the smallest and floating-point value that can be held by a particular C/C++ float or double variable.

FLT_MIN Minimum value a float can hold
FLT_MAX Maximum value float can hold
FLT_EPSILON Smallest positive value representable as a float
DBL_MIN Minimum value a double can hold
DBL_MAX Maximum value double can hold
DBL_EPSILON Smallest positive value representable as a double
LDBL_MIN Minimum value a long double can hold
LDBL_MAX Maximum value long doublet can hold
LDBL_EPSILON Smallest positive value representable as a long double
Copyright © 2004-2013 Paul J. Drongowski