The Standard C Library

This page contains a brief summary of some useful functions found in the standard C Library. The functions are organized according to the header file in which they are found. This list is not exhaustive, nor is it meant to fully describe each function. For greater understanding of any of these functions, consult the man pages for that function. Looking at the header files themselves (found in /usr/include) can also be very informative.

<assert.h>

Only one function here, void assert(int ). If the value in the argument is non-zero, nothing exciting happens. The program continues on. If the argument evaluates to zero, the program dies after printing a message on the standard error file (usually the screen).

The ONLY reasonable use for this function is in debugging. You sprinkle assert()s throughout the program, giving as the argument an expression that you think should be true at that point in the program. When the program runs, it evaluates your assertion (expression). If you were wrong, the program tells you where it's at, dies, and you get to figure out why your logic was wrong at that point. A very useful function.

But wait, there's more! If you #define NDEBUG before #include <assert.h> all of your assert()s are cheerfully ignored. So you can enable or disable all of your debugging checking by changing a single line.

<ctype.h>

This file declares functions useful for testing if characters fall in certain common categories. All of these functions take an unsigned character value or EOF (OK OK, they really take an int) and return a non-zero or zero value according to whether the character meets or does not meet the given criterion. These functions are usually implemented as lookup tables optimized for each specific architecture, so calling isupper(c) is much faster than if(c >= 'A' && c <='Z') ... .
isalnum(c)
a letter or numeric digit
isalpha(c)
a letter
iscntrl(c)
a control character
isdigit(c)
a decimal digit
isgraph(c)
a printable character but not white space
islower(c)
a lower-case letter
isprint(c)
a printable character, including white space
ispunct(c)
anything printable except letters, numbers, or white space (almost everything!)
isspace(c)
white space, i.e. space, tab, line feed, carriage return, form feed, vertical tab.
isupper(c)
an upper-case letter
isxdigit(c)
a hexadecimal digit
The following two functions convert characters to lower and upper case respectively when possible. If the character can't be converted, it is returned as is.
int tolower(int c)
converts c to lower case
int toupper(int c)
converts c to upper case

<limits.h>

This file specifies the minimum and maximum values for various integer types. The values are specific to the machine architecture, including the operating system. In other words, if you go to a machine like this one, the limits should be the same. If you go to a different machine, they might be the same. For example, the maximum value of an unsigned int is 65535 on a PC running DOS, and 4294967295 on a PC running Unix. The maxmimum value of an unsigned long int is 4294967295 on a Pentium class machine running Unix and 18446744073709551615 on a DEC Alpha machine running Unix.

Many programs break down when ported to different architectures because they make assumptions about the capacity of fundamental types. Using this header file helps eliminate that problem.

These values are all #defined in <limits.h>

SCHAR_MIN & SCHAR_MAX
limits for signed characters.
UCHAR_MAX
limit for unsigned characters.
CHAR_MIN & CHAR_MAX
limits for characters.
SHRT_MIN & SHRT_MAX
limits for signed short integers.
USRHT_MAX
limit for unsigned short integers.
INT_MIN & INT_MAX
limits for integers.
UINT_MAX
limit for unsigned integers.
LONG_MIN & LONG_MAX
limits for (signed) long integers.
ULONG_MAX
limit for unsigned integers.

<float.h>

This file is the same as <limits.h> except for floating point types instead of integers. These #defined values can vary according to architecture. Very important for portability of numeric processing programs.
FLT_MIN & FLT_MAX
limits for floats.
DBL_MIN & DBL_MAX
limits for doubles.
FLT_EPSILON & DBL_EPSILON
the smallest number x such that 1+x is different from 1. This is important in scientific calculations and helps determine how much accuracy you can expect out of your numeric algorithms.

<stdio.h>

<stdlib.h>

This file contains numerous functions, including conversions from strings to numbers, random number generation, dynamic memory manipulation, and sorting. It's a hodgepodge of useful routines that don't fit well in other files.
double atof(char *), int atoi(char *), and long atol(char *)
convert a numeric string into a double, an integer and a long integer respectively. These functions are very useful for converting numeric values from command line arguments.
double strtod(), long strtol(), and strtoul()
more robust versions of atof() and friends. The integer versions are able to convert numbers of different bases. Each of the str... functions also returns a pointer to the location in the string where it stopped the conversion.
int rand(void)
returns a pseudo-random integer in the range of 0 to RAND_MAX which is a symbolic constant also defined in this file.
void srand(unsigned int)
resets the random number generator using the integer passed in as a "seed". Using the same seed lets a program use the same sequence of random numbers, which gives a program reproducible behavior. Important for many simulations. Changing the seed also forces a new random number sequence, also useful for simulations.
void * malloc(int)
dynamically allocates the number of bytes indicated from system memory and returns a pointer to this new memory. The memory is uninitialized. If for some reason, the memory can not be allocated, malloc() returns NULL.
void * calloc(int n_obj, int size)
dynamically allocates memory to hold n_obj objects, each containing size bytes. Also initializes the allocated memory to all zeros. Much faster than using malloc() and then writing your own loop to clear the memory. If you have to initialize the memory to some complex value, calloc() is a waste of time.
void * realloc(void * p, int size)

<string.h>