fgetc() Read one character from a stream

fgetc() Read one character from a stream

fgetc() returns an unsigned char as int, or EOF at end-of-file or on error. Store it in int; use feof() and ferror() to distinguish the cause.

Categorystdio.h I/O
KindLibrary function
Header<stdio.h>
C standardC89 / C90

📝 Syntax

int fgetc(FILE *stream);

⚙️ Parameters

stream RequiredAn open stream available for input.

Returns: Returns the character read as an unsigned char converted to int, or EOF on end-of-file or a read error.

▶ Example

Edit the C code below and click Run; the server compiles and executes it:

Output:

A B
⚠️ Warning Store the result in an int and test for EOF before converting it to char; storing it directly in char can lose the end-of-file or error indication.
💡 Tip The example uses an automatically removed temporary file and calls rewind() to return to the beginning before reading.

📚 Related Tutorials

❓ FAQ

QWhy does fgetc return int instead of char?
AIt must represent every unsigned char value plus EOF. An int can hold both the character values and the negative EOF indicator.
QHow do I distinguish end-of-file from a read error when fgetc returns EOF?
AInspect the stream with feof() and ferror(): the former reports end-of-file, while the latter reports an I/O error.
QWhat is the difference between fgetc and getchar?
Agetchar() is equivalent to fgetc(stdin) and reads only standard input; fgetc() accepts any open input stream, so the same character-by-character logic can read files or stdin. Both return int, and callers must save the result and check for EOF.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏