1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>Standard Library Functions
</title>
10 <link href=
"sx14e.html" rev=precedes
>
11 <link href=
"sx14.html" rev=subdocument
>
14 <H2>Standard Library Functions
</H2>
16 <p>C's standard library contains many features and functions
17 which we haven't seen.
18 </p><p>We've seen many of
<TT>printf
</TT>'s formatting capabilities,
20 Besides format specifier characters for a few types we haven't seen,
22 you can also control the width, precision,
23 justification (left or right)
24 and a few other attributes of
<TT>printf
</TT>'s format conversions.
25 (In their full complexity,
26 <TT>printf
</TT> formats are about as elaborate and powerful
27 as FORTRAN
<TT>format
</TT> statements.)
28 </p><p>A
<TT>scanf
</TT> function lets you do ``formatted input''
29 analogous to
<TT>printf
</TT>'s formatted output.
30 <TT>scanf
</TT> reads from the standard input;
31 a variant
<TT>fscanf
</TT> reads from a specified file pointer.
33 <TT>sprintf
</TT> and
<TT>sscanf
</TT>
36 ``print'' and ``read''
37 to and from in-memory strings instead of files.
38 We've seen that
<TT>atoi
</TT> lets you convert a numeric
39 string into an integer;
40 the inverse operation can be performed with
<TT>sprintf
</TT>:
44 sprintf(str,
"%d", i);
46 </p><p>We've used
<TT>printf
</TT> and
<TT>fprintf
</TT> to write formatted output,
47 and
<TT>getchar
</TT>,
<TT>getc
</TT>,
<TT>putchar
</TT>, and
<TT>putc
</TT>
48 to read and write characters.
49 There are also functions
50 <TT>gets
</TT>,
<TT>fgets
</TT>,
<TT>puts
</TT>, and
<TT>fputs
</TT>
51 for reading and writing lines
52 (though we rarely need these,
53 especially if we're using our own
<TT>getline
</TT>
54 and maybe
<TT>fgetline
</TT>),
55 and also
<TT>fread
</TT> and
<TT>fwrite
</TT>
56 for reading or writing arbitrary numbers of characters.
57 </p><p>It's possible to ``un-read'' a character,
59 to push it back on an input stream, with
<TT>ungetc
</TT>.
60 (This is useful if you accidentally read one character too far,
61 and would prefer that some other part of your program
62 read that character instead.)
63 </p><p>You can use the
64 <TT>ftell
</TT>,
<TT>fseek
</TT>, and
<TT>rewind
</TT> functions
65 to jump around in files,
66 performing random access (as opposed to sequential) I/O.
67 </p><p>The
<TT>feof
</TT> and
<TT>ferror
</TT> functions will tell you
68 whether you got
<TT>EOF
</TT> due to an actual end-of-file
69 condition or due to a read error of some sort.
70 You can clear errors and end-of-file conditions with
72 </p><p>You can open files in ``binary'' mode, or for
76 (These options involve extra characters
77 appended to
<TT>fopen
</TT>'s mode string:
78 <TT>b
</TT> for binary,
79 <TT>+
</TT> for read/write.)
80 </p><p>There are several more string functions in
<TT><string.h
></TT>.
81 A second set of string functions
82 <TT>strncpy
</TT>,
<TT>strncat
</TT>, and
<TT>strncmp
</TT>
84 accept a third argument telling them to
85 stop after
<TT>n
</TT> characters
86 if they haven't found the
<TT>\
0</TT> marking the end of the string.
87 A third set of ``
<TT>mem
</TT>'' functions,
88 including
<TT>memcpy
</TT> and
<TT>memcmp
</TT>,
89 operate on blocks of memory
90 which aren't necessarily strings
91 and where
<TT>\
0</TT> is not treated as a terminator.
92 The
<TT>strchr
</TT> and
<TT>strrchr
</TT> functions
93 find characters in strings.
94 There is a motley collection
95 of ``span'' and ``scan'' functions,
96 <TT>strspn
</TT>,
<TT>strcspn
</TT>, and
<TT>strpbrk
</TT>,
97 for searching out or skipping over sequences of characters all
98 drawn from a specified set of characters.
99 The
<TT>strtok
</TT> function aids in breaking up a string
100 into words or ``tokens,''
101 much like our own
<TT>getwords
</TT> function.
102 </p><p>The header file
<TT><ctype.h
></TT> contains several functions
103 which let you classify and manipulate characters:
104 check for letters or digits,
105 convert between upper- and lower-case,
107 </p><p>A host of mathematical functions
108 are defined in the header file
<TT><math.h
></TT>.
109 (As we've mentioned, besides including
<TT><math.h
></TT>,
110 you may on some Unix systems
111 have to ask for a special library containing the math functions
112 while compiling/linking.)
113 </p><p>There's a random-number generator,
<TT>rand
</TT>,
114 and a way to ``seed'' it,
<TT>srand
</TT>.
115 <TT>rand
</TT> returns integers from
0 up to
<TT>RAND_MAX
</TT>
116 (where
<TT>RAND_MAX
</TT> is a constant
117 <TT>#define
</TT>d in
<TT><stdlib.h
></TT>).
118 One way of getting random integers from
1 to
<TT>n
</TT> is to call
120 (int)(rand() / (RAND_MAX +
1.0) * n) +
1
124 rand() / (RAND_MAX / n +
1) +
1
126 It seems like it would be simpler to just say
130 but this method is imperfect
131 (or rather, it's imperfect if
132 <TT>n
</TT> is a power of two
134 your system's implementation of
<TT>rand()
</TT> is imperfect,
135 as all too many of them are).
136 </p><p>Several functions let you interact with
137 the operating system under which your program is running.
138 The
<TT>exit
</TT> function
139 returns control to the operating system immediately,
140 terminating your program
141 and returning an ``exit status.''
142 The
<TT>getenv
</TT> function allows you to read
143 your operating system's
145 ``environment variables''
147 The
<TT>system
</TT> function
148 allows you to invoke an operating-system command
149 (i.e. another program)
150 from within your program.
151 </p><p>The
<TT>qsort
</TT> function allows you to sort an array
153 you supply a comparison function
154 (via a function pointer)
155 which knows how to compare two array elements,
156 and
<TT>qsort
</TT> does the rest.
157 The
<TT>bsearch
</TT> function allows you to
158 search for elements in sorted arrays;
159 it, too, operates in terms of a caller-supplied comparison function.
160 </p><p>Several functions--
<TT>time
</TT>,
168 <TT>strftime
</TT>--allow
169 you to determine the current date and time,
170 print dates and times,
171 and perform other date/time manipulations.
172 For example, to print today's date in a program,
175 #include
<time.h
>
178 now = time((time_t *)NULL);
179 printf(
"It's %.24s", ctime(
&now));
181 </p><p>The header file
<TT><stdarg.h
></TT> lets you manipulate
182 variable-length function argument lists
183 (such as the ones
<TT>printf
</TT> is called with).
184 Additional members of the
<TT>printf
</TT> family of functions
185 let you write your own functions which accept
186 <TT>printf
</TT>-like format specifiers and
187 variable numbers of arguments but call on the
188 standard
<TT>printf
</TT> to do most of the work.
189 </p><p>There are facilities for dealing with
190 multibyte and ``wide'' characters and strings,
191 for use with multinational character sets.
195 <a href=
"sx14e.html" rev=precedes
>prev
</a>
196 <a href=
"sx14.html" rev=subdocument
>up
</a>
197 <a href=
"top.html">top
</a>
200 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
201 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
202 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>