xref: /freebsd/lib/libc/stdio/getc.3 (revision e2340276fc734a1f0bd0d2cf16fcfba7936c9462)
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.\"     @(#)getc.3	8.1 (Berkeley) 6/4/93
33.\"
34.Dd April 4, 2020
35.Dt GETC 3
36.Os
37.Sh NAME
38.Nm fgetc ,
39.Nm getc ,
40.Nm getc_unlocked ,
41.Nm getchar ,
42.Nm getchar_unlocked ,
43.Nm getw
44.Nd get next character or word from input stream
45.Sh LIBRARY
46.Lb libc
47.Sh SYNOPSIS
48.In stdio.h
49.Ft int
50.Fn fgetc "FILE *stream"
51.Ft int
52.Fn getc "FILE *stream"
53.Ft int
54.Fn getc_unlocked "FILE *stream"
55.Ft int
56.Fn getchar void
57.Ft int
58.Fn getchar_unlocked void
59.Ft int
60.Fn getw "FILE *stream"
61.Sh DESCRIPTION
62The
63.Fn fgetc
64function
65obtains the next input character (if present) from the stream pointed at by
66.Fa stream ,
67or the next character pushed back on the stream via
68.Xr ungetc 3 .
69.Pp
70The
71.Fn getc
72function
73acts essentially identically to
74.Fn fgetc ,
75but is a macro that expands in-line.
76.Pp
77The
78.Fn getchar
79function
80is equivalent to
81.Fn getc stdin .
82.Pp
83The
84.Fn getw
85function
86obtains the next
87.Vt int
88(if present)
89from the stream pointed at by
90.Fa stream .
91.Pp
92The
93.Fn getc_unlocked
94and
95.Fn getchar_unlocked
96functions are equivalent to
97.Fn getc
98and
99.Fn getchar
100respectively,
101except that the caller is responsible for locking the stream
102with
103.Xr flockfile 3
104before calling them.
105These functions may be used to avoid the overhead of locking the stream
106for each character, and to avoid input being dispersed among multiple
107threads reading from the same stream.
108.Sh RETURN VALUES
109If successful, these routines return the next requested object
110from the
111.Fa stream .
112Character values are returned as an
113.Vt "unsigned char"
114converted to an
115.Vt int .
116If the stream is at end-of-file or a read error occurs,
117the routines return
118.Dv EOF .
119The routines
120.Xr feof 3
121and
122.Xr ferror 3
123must be used to distinguish between end-of-file and error.
124If an error occurs, the global variable
125.Va errno
126is set to indicate the error.
127The end-of-file condition is remembered, even on a terminal, and all
128subsequent attempts to read will return
129.Dv EOF
130until the condition is cleared with
131.Xr clearerr 3 .
132.Sh SEE ALSO
133.Xr ferror 3 ,
134.Xr flockfile 3 ,
135.Xr fopen 3 ,
136.Xr fread 3 ,
137.Xr getwc 3 ,
138.Xr putc 3 ,
139.Xr ungetc 3
140.Sh STANDARDS
141The
142.Fn fgetc ,
143.Fn getc ,
144and
145.Fn getchar
146functions
147conform to
148.St -isoC .
149The
150.Fn getc_unlocked
151and
152.Fn getchar_unlocked
153functions conform to
154.St -p1003.1-2001 .
155.Sh HISTORY
156The
157.Fn getc
158and
159.Fn getw
160functions appeared in a similar form in
161.At v1 ;
162and were integrated into stdio in
163.At v7 ;
164.Fn getchar
165in
166.At v4 ;
167and
168.Fn fgetc
169in
170.At v7 .
171.Sh BUGS
172Since
173.Dv EOF
174is a valid integer value,
175.Xr feof 3
176and
177.Xr ferror 3
178must be used to check for failure after calling
179.Fn getw .
180The size and byte order of an
181.Vt int
182varies from one machine to another, and
183.Fn getw
184is not recommended for portable applications.
185