fgetc(3)

โœจ fgetc โ€” the manual, but make it make sense โœจ

okay bestie, welcome to the fgetc(3) page ๐Ÿ’– it lives in the Library Functions Manual, and it's literally the origin story of fgetc, getchar, and ungetc โ€” the trio that reads single characters out of a FILE. yes, one character at a time. no, that's not a crime, it's a vibe. ๐ŸŒธ


๐ŸŽ€ NAME

fgetc, getchar, ungetc โ€” grab a character from a FILE, that's the whole job.


๐Ÿ“š LIBRARY

Standard C library, aka libc, link it with -lc. classic. ๐Ÿ‘‘


๐Ÿฆ‹ SYNOPSIS

#include <stdio.h>

int fgetc(FILE *stream);
int getchar(void);

int ungetc(int c, FILE *stream);

like. three little functions. three little icons. periodt. โœจ


๐Ÿ’œ DESCRIPTION

fgetc() reads the next character out of stream and hands it back to you as an unsigned char cast to an int ๐Ÿ’… or it gives you EOF if you hit end of file or an error happens. that's it, that's the function.

getchar() is literally just fgetc(stdin). same energy, fewer arguments. it's giving "I didn't want to type stdin". ๐Ÿ™ˆ

ungetc() pushes c back onto stream, cast to unsigned char, where it waits patiently for the next read operation to come pick it up. so iconic of it. ๐Ÿซถ

โš ๏ธ okay important: pushed-back characters come back in reverse order, and only one pushback is guaranteed. ONE. so don't be out here pushing back a whole paragraph and expecting an orderly queue, bestie, that's not the deal. ๐Ÿ˜ญ

calls to these functions can be mixed with each other, and also with calls to other input functions from the stdio library, as long as it's the same input stream. they're chill like that. ๐ŸŒˆ

for nonlocking counterparts, go look at unlocked_stdio(3). ๐Ÿ”ฅ


๐Ÿ’– RETURN VALUE

  • fgetc() and getchar() โ€” return the character read as an unsigned char cast to an int, or EOF on end of file or error.
  • ungetc() โ€” returns c on success, or EOF on error.

and if something goes wrong, errno is set to tell you what's up. always check on your friends. โœจ


๐Ÿ˜ญ ERRORS

see read(2). go read it. it knows things. ๐Ÿ‘‘


๐Ÿงš ATTRIBUTES

for an explanation of the terms used in this section, see attributes(7).

  • Interface: fgetc(), getchar(), ungetc()
    • Attribute: Thread safety
    • Value: MT-Safe

MT-Safe, meaning they're safe to call from multiple threads. slay. ๐Ÿ’…


๐ŸŒŸ STANDARDS

C11, POSIX.1-2008. it's giving certified. it's giving committee-approved. ๐Ÿ‘‘


๐Ÿ•ฐ๏ธ HISTORY

POSIX.1-2001, C89. ancient. ancestral. the blueprint. ๐ŸŒธ


๐Ÿ“ NOTES

๐Ÿ’€ do NOT mix calls to input functions from the stdio library with low-level calls to read(2) on the file descriptor associated with the input stream. the results will be undefined, and very probably not what you want. like, not even a little bit what you want. bestie please. just don't. ๐Ÿ˜ญ

๐Ÿ‘€ SEE ALSO

read(2), write(2), ferror(3), fgetwc(3), fgets(3), fopen(3), fread(3), fseek(3), getline(3), gets(3), getwchar(3), puts(3), scanf(3), ungetwc(3), unlocked_stdio(3), feature_test_macros(7)


okay that's the whole page, you ate it, you understood it, you're basically a stdio main character now. ๐ŸŽ€๐Ÿ’–

this has been Linux man-pages 6.20, dated 2026-08-19, and fgetc(3) โ€” drink water, close your streams, bye bestie ๐ŸŒธ๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms