One of the reasons C is a popular language is that it is a "high level" "low level"
language. It provides high level programming constructs such as functions, loop, and test which make it easy
to write modular, well structured code.
At the same time, it provides low level operators and freedom to allow a programer to create their own tools
for manipulating their data.
Up until now, we have largely written our own tools when we needed them (e.g. chrutil.h). However, the standard C library provides a large number of built-in utility functions and macros we can use instead of writing our own. The full standard C library is documented in Appendix C of the textbook. The functions provided fall into several categories:
Character Class Tests
isalnum
alpha numeric (letter, digit)
isalpha
alpha (letter)
iscntrl
control
isdigit
digit
isgraph
graphic (printable except for space)
islower
lower case letter
isprint
printable
ispunct
punctuation
isspace
white space
isupper
upper case letter
isxdigit
hexadecimal digit
tolower
lower case
toupper
upper case
strcpy
copies a string to another
strncpy
copies n characters from a string to another
strcat
concatenates two strings
strncat
concatenates n characters from a string onto another
strcmp
compares two strings
strncmp
compares n characters of two strings
strchr
returns the first occurrence of character in a string
strrchr
returns the last occurrence of character in a string
strspn
returns the length of leading characters in a string that are contained in a specified string
strcspn
returns the length of leading characters in a string not contained in a specified string
strpbrk
returns first occurrence in a string of any character in another string
strstr
returns the first occurrence of one string in another
strlen
returns length of a string
strerror
returns a string corresponding to an error number
strtok
returns a token from a string delimited by specified characters
Memory Functions (Memory treated as an array):
memcpy
copies n characters from one address to another
memmove
same as memcpy, except that overlap is allowed
memcmp
compares n characters at specified addresses
memchr
returns first occurrence of a character in a string at an address
memset
stores a character in first n positions starting at specified address
Utility Functions including system().
The system() function is just a convenient way to execute a command from within a C program. The prototype is:
int system(char *s);
In Unix, the string, s, is given to a shell to run. When the command completes, system() returns.
For example, one way I implemented the startup screen in the miensfeld game was to use:
system("cat title1");
atof
converts a string to a float
atoi
converts a string to an integer
atol
converts a string to a long
strtod
converts a string to double
strtol
converts a string to long
strtoul
converts a string to unsigned long
rand
generates a random integer
srand
seeds the random generator
Dynamic memory allocation:
calloc
dynamically allocates memory for n items of size
malloc
dynamically allocates memory for n bytes
realloc
reallocates a block of memory with a different size
free
frees previously allocated memory
abort
abnormally terminates a program
exit
normally terminates a program
system
executes system commands
bsearch
binary search function
qsort
sort function
abs
returns absolute value of an integer
labs
returns absolute value of a long
div
int type: returns quotient and remainder of x / y
ldiv
long type: similar to div
Math Functions
NOTE: to use the math library, you must include <math.h> AND link the math library on the cc line:
cc whatever.c -o whatever -lm
sin
sine
cos
cosine
tan
tangent
asin
arc sine
acos
arc cosine
atan
arc tangent, range first quadrant
atan2
arc tangent, range two quadrants
sinh
hyperbolic sine
cosh
hyperbolic cosine
tanh
hyperbolic tangent
exp
exponential
log
natural logarithm
log10
base 10 logarithm
pow
yth power of x
sqrt
square root
ceil
smallest whole number greater than x
floor
largest whole number less than x
fabs
absolute value of a floating point
ldexp
generates a base 2 fraction times an exponent
frexp
splits a number into a base 2 fraction and an exponent
modf
splits a number into fractional and integral parts
fmod
floating point remainder of x / y, x and y are double
fopen
opens a file stream
freopen
open a file and associate with a stream
fflush
flushes an output file stream buffer
fclose
closes a file stream
remove
removes a named file
rename
renames an old file name to a new name
tmpfile
creates a temporary file
setbuf
sets a file stream as buffered or unbuffered
fprintf
writes to a file
printf
writes to standard output
sprintf
writes to a string
fscanf
reads from a file
scanf
reads from standard input
sscanf
reads from a string
Character and String I/O:
fgetc
reads a character from a file
fgets
reads a string from a file
fputc
writes a character to a file
fputs
writes a string to a file
getc
reads a character from a file
getchar
reads a character from standard input
gets
reads a string from standard input
putc
writes a character to a file
putchar
writes a character to standard output
puts
writes a string to standard output
ungetc
puts a character back onto a stream; only one character per stream may be allowed to be put back
fread
reads a block from a file
fwrite
writes a block to a file
File Pointer Positioning:
fseek
seeks a position in a file stream
ftell
returns a position in a stream
rewind
essentially reopens a file
fgetpos
gets current position in a stream
fsetpos
sets the current position in a stream
clearerr
clears error and end of file indicators
feof
chaecks end of file indicator
ferror
checks file error indicator
perror
prints a specified string and an error message for errno
Limit Macros
Many implementation dependent sizes are defined in these files. The following are examples of some of them. The values specified are mimimum values; larger values are acceptable.
Others
Thre are several other library functions and header files of limited use that we have not included here. Readers should refer to appropriate manuals. Some of the categories are:
Declares integer type errno which will contain the number of an error when it occurs.
Provides facilities to handle exceptional situations such as interrupts or errors.
Date and Time Functions <time.h>
Provides clock functions as well as date and time functions.
Variable Argument Lists <stdarg.h>
Provides failities to step through a list of function arguments of unknown number and type.
Non-local Jumps <setjmp.h>
Provides a way of avoiding normal function calls and returns.
A full alphabetic listing of the C library is also found in the Appendix of your Textbook.
C library functions are also documented on-line, for example here (you just have to put up with the ads).