xref: /freebsd/lib/libc/stdio/getc.3 (revision af0a81b6470aba4af4a24ae9804053722846ded4)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.Dd April 4, 2020
33.Dt GETC 3
34.Os
35.Sh NAME
36.Nm fgetc ,
37.Nm getc ,
38.Nm getc_unlocked ,
39.Nm getchar ,
40.Nm getchar_unlocked ,
41.Nm getw
42.Nd get next character or word from input stream
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft int
48.Fn fgetc "FILE *stream"
49.Ft int
50.Fn getc "FILE *stream"
51.Ft int
52.Fn getc_unlocked "FILE *stream"
53.Ft int
54.Fn getchar void
55.Ft int
56.Fn getchar_unlocked void
57.Ft int
58.Fn getw "FILE *stream"
59.Sh DESCRIPTION
60The
61.Fn fgetc
62function
63obtains the next input character (if present) from the stream pointed at by
64.Fa stream ,
65or the next character pushed back on the stream via
66.Xr ungetc 3 .
67.Pp
68The
69.Fn getc
70function
71acts essentially identically to
72.Fn fgetc ,
73but is a macro that expands in-line.
74.Pp
75The
76.Fn getchar
77function
78is equivalent to
79.Fn getc stdin .
80.Pp
81The
82.Fn getw
83function
84obtains the next
85.Vt int
86(if present)
87from the stream pointed at by
88.Fa stream .
89.Pp
90The
91.Fn getc_unlocked
92and
93.Fn getchar_unlocked
94functions are equivalent to
95.Fn getc
96and
97.Fn getchar
98respectively,
99except that the caller is responsible for locking the stream
100with
101.Xr flockfile 3
102before calling them.
103These functions may be used to avoid the overhead of locking the stream
104for each character, and to avoid input being dispersed among multiple
105threads reading from the same stream.
106.Sh RETURN VALUES
107If successful, these routines return the next requested object
108from the
109.Fa stream .
110Character values are returned as an
111.Vt "unsigned char"
112converted to an
113.Vt int .
114If the stream is at end-of-file or a read error occurs,
115the routines return
116.Dv EOF .
117The routines
118.Xr feof 3
119and
120.Xr ferror 3
121must be used to distinguish between end-of-file and error.
122If an error occurs, the global variable
123.Va errno
124is set to indicate the error.
125The end-of-file condition is remembered, even on a terminal, and all
126subsequent attempts to read will return
127.Dv EOF
128until the condition is cleared with
129.Xr clearerr 3 .
130.Sh SEE ALSO
131.Xr ferror 3 ,
132.Xr flockfile 3 ,
133.Xr fopen 3 ,
134.Xr fread 3 ,
135.Xr getwc 3 ,
136.Xr putc 3 ,
137.Xr ungetc 3
138.Sh STANDARDS
139The
140.Fn fgetc ,
141.Fn getc ,
142and
143.Fn getchar
144functions
145conform to
146.St -isoC .
147The
148.Fn getc_unlocked
149and
150.Fn getchar_unlocked
151functions conform to
152.St -p1003.1-2001 .
153.Sh HISTORY
154The
155.Fn getc
156and
157.Fn getw
158functions appeared in a similar form in
159.At v1 ;
160and were integrated into stdio in
161.At v7 ;
162.Fn getchar
163in
164.At v4 ;
165and
166.Fn fgetc
167in
168.At v7 .
169.Sh BUGS
170Since
171.Dv EOF
172is a valid integer value,
173.Xr feof 3
174and
175.Xr ferror 3
176must be used to check for failure after calling
177.Fn getw .
178The size and byte order of an
179.Vt int
180varies from one machine to another, and
181.Fn getw
182is not recommended for portable applications.
183