C Libraries Bart Childs Complementary to the text(s) 2006 C was designed to make extensive use of a number of libraries. A great reference for student purposes is appendix B of the K&R book. This list is ended to be a subset of those described there but to include those most likely to be used in an (not too) elementary course. The standard headers include: <assert.h> <float.h> <math.h> <stdarg.h> <stdlib.h> <ctype.h> <limits.h> <setjump.h> <stddef.h> <string.h> <errbi,g> <locale.h> <signal.h> <stdio.h> <time.h> Notice the order is quite unix like, alphabetical down the column. The order of the following sections will be somewhat like the earliest occurrence of the student needing them. The type size t is the unsigned egral type returned by the sizeof operator. That is subtle, sizeof is an operator therefore it is not in a standard library. The files stdin and stdout are the default files for eractive usage. They can be redirected.
1 <stdio.h> In K&R there are many subsections for this. In K&P there are smatterings here and there and then chapter 13 is full blown. Only a few of the many are included here. The first pair of input/output commands is the default ones and two extended pairs follow. Then some basic file commands are included. prf (const char format, list ) converts and writes to the output stream ( stdout in this case) under the control of the format. The return value is the number of characters written or is negative if an error has occurred. scanf (const char format, target s ) assigns converted values to the memory poed to by the target s, which are poers. scanf () returns when the format is exhausted. The return value is EOF if an end of file or error is encountered otherwise the number of conversions completed and assigned is returned. The char format argument in these most basic input/output commands seems to be a string constant in most cases. It can be a string variable and the user can create these in the program based on the data. The format for the input and output statements are quite similar but not identical. For example scanf("%lf", &dble); prf("%15.7lf", dble); can be used to input a value of type double from the standard input medium and then output it to the standard output medium. The output is specified to be 15 characters wide with 7 significant digits in the mantissa. The input is found in the input stream with appropriate delimiting, usually by whitespace. char scanfs prfs c character: char *. character: char. d decimal eger: * signed decimal eger lf floating: double * l is not needed. i eger: *. May be decimal, same as d octal, or hexadecimal. p void **. Input what is output. void *. Hexadecimal. 2
Finish tables of prf () and scanf () conversions and flags for format, similar to the above short one. The ssprf () and sscanf () functions are quite similar to the previous functions except that the input/output is to the first argument which is a string. The format and subsequent arguments work the same in all three of scanf () and prf () functions. sprf (char s, const char format, \ ldots {}) converts and writes to the output stream ( stdout in this case) under the control of the format. The return value is the number of characters written or is negative if an error has occurred. The output stream is the first argument, a string. sscanf (char s, const char format, \ ldots {}) assigns converted values to the memory poed to by the subsequent arguments which are poers. The input is from the first argument, a string. sscanf () returns when the format is exhausted. The return value is EOF if an end of file or error is encountered otherwise the number of conversions completed and assigned is returned. The fprf () and fscanf () functions are quite similar to the previous except the input file poer is included as the first argument. fprf (FILE stream, const char format, \ ldots {}) converts and writes to the output stream under the control of the format. The return value is the number of characters written or is negative if an error has occurred. fscanf (FILE stream, const char format, \ ldots {}) assigns converted values to the subsequent arguments (to the memory poed to by these poers). fscanf () returns when the format is exhausted. The return value is EOF if an end of file or error is encountered otherwise the number of conversions completed and assigned is returned. The concepts of C are surprising to many programmers who have had some experience with other languages. C is oriented to the concept of getting input a character at a time. We often think of a line of input at a time. The following functions enable the line at a time concepts. 3
char gets ( char s) Do not use this function, instead use the next one for safety and flexibilty. char fgets ( char s ), n, FILE stream) is similar to gets () with three notable exceptions. The first is that there are two more arguments. The argument n is a maximum number of characters that will be input and finally the input file is named by the third argument. If the number of characters input is less than n, then the newline character is preserved and a \0 is appended. The most basic of the file functions enable the user to associate input/output with specific files and remove that association when the program exits. FILE fopen(const char filename, const char mode) opens the filename given as the first argument and returns the file poer. This shall be captured by using this function as the right side of an assignment statement. The usual modes are "r", "w", and "rw" for read, write, and read/write. fclose (FILE stream) returns, etc. FILE freopen(const char filename, const char mode, FILE fp) closes the open file and then opens the stream again with the file name specified in the first argument. The return value is the FILE. Note that if the file is stdin, stdout, or stderr these file poers cannot be on the left side of an assignment statement because they are constants. Wow! The values of these constants are changed in the freopen () statement. feof (FILE fp ); returns a nonzero value if an end of file has been encountered for the file associated with fp. Another way of capturing the same situation is to assign the value of the return from the scanf ()s and testing it against EOF. ferror (FILE fp); returns a nonzero value if an error flag has been raised for the file associated with fp. If input is being done by use of fgets () one can use ferror () and feof () to determine more precise information than the simple returning of a NULL from fgets (). 4
2 <ctype.h> The character class tests are descriptive in their names if you know the vocabulary. These return true (non-zero) or false (zero). isalnum( c) alphanumeric isalpha ( c) alphabetic iscntrl ( c) control character isdigit ( c) decimal digit isgraph( c) pring character except space (ASCII) islower ( c) lower-case letter ispr ( c) pring character including space (ASCII) ispunct ( c) pring character except space, letter, or digit isspace ( c) space, formfeed,f newline, carriage return, tabs isupper ( c) upper-case letter isxdigit ( c) hexadecimal digit Case changes are performed by these two functions. Return the input if already appropriate or not a letter. toupper( c) change to an uppercase if it is lowercase tolower( c) change to a lowercase if it is uppercase 3 <string.h> Strings are not really a type in C. They are arrays of the type char and the functions involving them require a special characteristic. That is there is a null byte denoting the end of the string. If the string has n characters, then its declaration is like char string [n + 1] and the indexes of the elements are 0...n 1 for the data and n for the null byte. 5
The following is adapted from section B.3 of K&R. This does not include all the function headers described in that section. Variables s and t are of type char ; and variables st and ct are of type const char ;. The variable n is of type size t and c is an converted to char. These functions have been used in class or about to be used. char strcpy ( s, ct ) copy string ct to string s, including trailing \0 ; return s. char strncpy ( s, ct, n) copy at most n characters of ct to s; return s. Pad with \0 s if ct has fewer than n characters. char strcat ( s, ct ) concatenate string ct to end of string s; terminate with \0 ; return s. char strncat ( s, ct,n) concatenate at most n characters string ct to end of string s; terminate with \0 ; return s. strcmp( cs, ct ) compare string cs to string ct; return <0 if cs<ct, 0 if identical; or >0 if cs>ct; strncmp( cs, ct,n) compare at most n characters of string cs to string ct; return <0 if cs<ct, 0 if identical; or >0 if cs>ct; char strchr ( cs, c) return poer (not index) to first occurrence of c in cs; NULL if not present. char strrchr ( cs, c) return poer (not index) to last occurrence of c in cs; NULL if not present. char strstr ( cs, ct ) return poer to first occurrence of string ct in cs, NULL if not present. size t strlen ( cs) return length of cs. 6
4 <math.h> 5 <stdlib.h> void calloc ( size t n, size t el size ); void qsort ( void a ptr, size t n els, size t el size, compare( const void, const void )); The use of qsort () is powerful and must be done with a bit of care. 6 <assert.h> 7 <stdarg.h> 8 <setjmp.h> 9 <signal.h> 10 <time.h> 11 <limits.h> 12 <float.h> 13 Bibliography K&P Kelly and Pohl, C by Dissection, 4 th ed, Addison-Wesley, 2001. K&R Kernighhan, Brian and Ritchie, Dennis, The C Programming Language, 2 nd ed, Prentice Hall, 1988. The first edition was copyright in 1978. The second was published as the ANSI standard for C was about to appear. 7