1FGETC(3) Linux Programmer's Manual FGETC(3)
2
3
4
6 fgetc, fgets, getc, getchar, ungetc - input of characters and strings
7
9 #include <stdio.h>
10
11 int fgetc(FILE *stream);
12 int getc(FILE *stream);
13 int getchar(void);
14
15 char *fgets(char *restrict s, int size, FILE *restrict stream);
16
17 int ungetc(int c, FILE *stream);
18
20 fgetc() reads the next character from stream and returns it as an un‐
21 signed char cast to an int, or EOF on end of file or error.
22
23 getc() is equivalent to fgetc() except that it may be implemented as a
24 macro which evaluates stream more than once.
25
26 getchar() is equivalent to getc(stdin).
27
28 fgets() reads in at most one less than size characters from stream and
29 stores them into the buffer pointed to by s. Reading stops after an
30 EOF or a newline. If a newline is read, it is stored into the buffer.
31 A terminating null byte ('\0') is stored after the last character in
32 the buffer.
33
34 ungetc() pushes c back to stream, cast to unsigned char, where it is
35 available for subsequent read operations. Pushed-back characters will
36 be returned in reverse order; only one pushback is guaranteed.
37
38 Calls to the functions described here can be mixed with each other and
39 with calls to other input functions from the stdio library for the same
40 input stream.
41
42 For nonlocking counterparts, see unlocked_stdio(3).
43
45 fgetc(), getc(), and getchar() return the character read as an unsigned
46 char cast to an int or EOF on end of file or error.
47
48 fgets() returns s on success, and NULL on error or when end of file oc‐
49 curs while no characters have been read.
50
51 ungetc() returns c on success, or EOF on error.
52
54 For an explanation of the terms used in this section, see at‐
55 tributes(7).
56
57 ┌────────────────────────────────────────────┬───────────────┬─────────┐
58 │Interface │ Attribute │ Value │
59 ├────────────────────────────────────────────┼───────────────┼─────────┤
60 │fgetc(), fgets(), getc(), getchar(), │ Thread safety │ MT-Safe │
61 │ungetc() │ │ │
62 └────────────────────────────────────────────┴───────────────┴─────────┘
63
65 POSIX.1-2001, POSIX.1-2008, C89, C99.
66
67 It is not advisable to mix calls to input functions from the stdio li‐
68 brary with low-level calls to read(2) for the file descriptor associat‐
69 ed with the input stream; the results will be undefined and very proba‐
70 bly not what you want.
71
73 read(2), write(2), ferror(3), fgetwc(3), fgetws(3), fopen(3), fread(3),
74 fseek(3), getline(3), gets(3), getwchar(3), puts(3), scanf(3),
75 ungetwc(3), unlocked_stdio(3), feature_test_macros(7)
76
78 This page is part of release 5.12 of the Linux man-pages project. A
79 description of the project, information about reporting bugs, and the
80 latest version of this page, can be found at
81 https://www.kernel.org/doc/man-pages/.
82
83
84
85GNU 2021-03-22 FGETC(3)