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
isalnumalpha numeric (letter, digit)isalphaalpha (letter)iscntrlcontrolisdigitdigitisgraphgraphic (printable except for space)islowerlower case letterisprintprintableispunctpunctuationisspacewhite spaceisupperupper case letterisxdigithexadecimal digit
tolowerlower casetoupperupper case
strcpycopies a string to anotherstrncpycopies n characters from a string to anotherstrcatconcatenates two stringsstrncatconcatenates n characters from a string onto anotherstrcmpcompares two stringsstrncmpcompares n characters of two stringsstrchrreturns the first occurrence of character in a stringstrrchrreturns the last occurrence of character in a stringstrspnreturns the length of leading characters in a string that are contained in a specified stringstrcspnreturns the length of leading characters in a string not contained in a specified stringstrpbrkreturns first occurrence in a string of any character in another stringstrstrreturns the first occurrence of one string in anotherstrlenreturns length of a stringstrerrorreturns a string corresponding to an error numberstrtokreturns a token from a string delimited by specified characters
Memory Functions (Memory treated as an array):
memcpycopies n characters from one address to anothermemmovesame as memcpy, except that overlap is allowedmemcmpcompares n characters at specified addressesmemchrreturns first occurrence of a character in a string at an addressmemsetstores 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");
atofconverts a string to a floatatoiconverts a string to an integeratolconverts a string to a longstrtodconverts a string to doublestrtolconverts a string to longstrtoulconverts a string to unsigned long
randgenerates a random integersrandseeds the random generator
Dynamic memory allocation:
callocdynamically allocates memory for n items of sizemallocdynamically allocates memory for n bytesreallocreallocates a block of memory with a different sizefreefrees previously allocated memory
abortabnormally terminates a programexitnormally terminates a program
systemexecutes system commands
bsearchbinary search functionqsortsort function
absreturns absolute value of an integerlabsreturns absolute value of a longdivint type: returns quotient and remainder of x / yldivlong 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
sinsinecoscosinetantangentasinarc sineacosarc cosineatanarc tangent, range first quadrantatan2arc tangent, range two quadrants
sinhhyperbolic sinecoshhyperbolic cosinetanhhyperbolic tangent
expexponentiallognatural logarithmlog10base 10 logarithm
powyth power of xsqrtsquare root
ceilsmallest whole number greater than xfloorlargest whole number less than x
fabsabsolute value of a floating pointldexpgenerates a base 2 fraction times an exponentfrexpsplits a number into a base 2 fraction and an exponentmodfsplits a number into fractional and integral partsfmodfloating point remainder of x / y, x and y are double
fopenopens a file streamfreopenopen a file and associate with a streamfflushflushes an output file stream bufferfclosecloses a file stream
removeremoves a named filerenamerenames an old file name to a new nametmpfilecreates a temporary filesetbufsets a file stream as buffered or unbuffered
fprintfwrites to a fileprintfwrites to standard outputsprintfwrites to a stringfscanfreads from a filescanfreads from standard inputsscanfreads from a string
Character and String I/O:
fgetcreads a character from a filefgetsreads a string from a filefputcwrites a character to a filefputswrites a string to a filegetcreads a character from a filegetcharreads a character from standard inputgetsreads a string from standard inputputcwrites a character to a fileputcharwrites a character to standard outputputswrites a string to standard outputungetcputs a character back onto a stream; only one character per stream may be allowed to be put back
freadreads a block from a filefwritewrites a block to a file
File Pointer Positioning:
fseekseeks a position in a file streamftellreturns a position in a streamrewindessentially reopens a filefgetposgets current position in a streamfsetpossets the current position in a stream
clearerrclears error and end of file indicatorsfeofchaecks end of file indicatorferrorchecks file error indicatorperrorprints 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).