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