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