xref: /netbsd/lib/libc/stdio/getc.3 (revision bf9ec67e)
1.\"	$NetBSD: getc.3,v 1.8 2002/02/07 07:00:25 ross 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. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"	This product includes software developed by the University of
21.\"	California, Berkeley and its contributors.
22.\" 4. Neither the name of the University nor the names of its contributors
23.\"    may be used to endorse or promote products derived from this software
24.\"    without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36.\" SUCH DAMAGE.
37.\"
38.\"     @(#)getc.3	8.1 (Berkeley) 6/4/93
39.\"
40.Dd April 25, 2001
41.Dt GETC 3
42.Os
43.Sh NAME
44.Nm fgetc ,
45.Nm getc ,
46.Nm getchar ,
47.Nm getc_unlocked ,
48.Nm getchar_unlocked ,
49.Nm getw
50.Nd get next character or word from input stream
51.Sh LIBRARY
52.Lb libc
53.Sh SYNOPSIS
54.Fd #include \*[Lt]stdio.h\*[Gt]
55.Ft int
56.Fn fgetc "FILE *stream"
57.Ft int
58.Fn getc "FILE *stream"
59.Ft int
60.Fn getchar
61.Ft int
62.Fn getc_unlocked "FILE *stream"
63.Ft int
64.Fn getchar_unlocked
65.Ft int
66.Fn getw "FILE *stream"
67.Sh DESCRIPTION
68The
69.Fn fgetc
70function
71obtains the next input character (if present) from the stream pointed at by
72.Fa stream ,
73or the next character pushed back on the stream via
74.Xr ungetc 3 .
75.Pp
76The
77.Fn getc
78function
79acts essentially identically to
80.Fn fgetc ,
81but is a macro that expands in-line.
82.Pp
83The
84.Fn getchar
85function
86is equivalent to:
87getc with the argument stdin.
88.Pp
89The
90.Fn getc_unlocked
91and
92.Fn getchar_unlocked
93functions provide functionality identical to that of
94.Fn getc
95and
96.Fn getchar ,
97respectively, but do not perform implicit locking of the streams they
98operate on.
99In multi-threaded programs they may be used
100.Em only
101within a scope in which the stream
102has been successfully locked by the calling thread using either
103.Xr flockfile 3
104or
105.Xr ftrylockfile 3 ,
106and may later be released using
107.Xr funlockfile 3 .
108.Pp
109The
110.Fn getw
111function
112obtains the next
113.Em int
114(if present)
115from the stream pointed at by
116.Fa stream .
117.Sh RETURN VALUES
118If successful, these routines return the next requested object
119from the
120.Fa stream .
121If the stream is at end-of-file or a read error occurs,
122the routines return
123.Dv EOF .
124The routines
125.Xr feof 3
126and
127.Xr ferror 3
128must be used to distinguish between end-of-file and error.
129If an error occurs, the global variable
130.Va errno
131is set to indicate the error.
132The end-of-file condition is remembered, even on a terminal, and all
133subsequent attempts to read will return
134.Dv EOF
135until the condition is cleared with
136.Xr clearerr 3 .
137.Sh SEE ALSO
138.Xr ferror 3 ,
139.Xr fopen 3 ,
140.Xr fread 3 ,
141.Xr putc 3 ,
142.Xr ungetc 3
143.Sh STANDARDS
144The
145.Fn fgetc ,
146.Fn getc
147and
148.Fn getchar
149functions
150conform to
151.St -ansiC .
152The
153.Fn getc_unlocked
154and
155.Fn getchar_unlocked
156functions conform to
157.St -p1003.1-96 .
158.Sh BUGS
159Since
160.Dv EOF
161is a valid integer value,
162.Xr feof 3
163and
164.Xr ferror 3
165must be used to check for failure after calling
166.Fn getw .
167The size and byte order of an
168.Em int
169varies from one machine to another, and
170.Fn getw
171is not recommended for portable applications.
172