C stdio.h Functions

Input and Output Through C Streams
<stdio.h> gives C a portable stream model. The same core operations can read a keyboard, write a terminal, process a text file, or transfer binary records. A FILE * stores the stream state, including buffering, position, end-of-file, and error indicators.
This catalog groups all 45 tutorials by job. Every guide identifies the declaration, valid arguments, exact success and failure results, the most important safety rule, and three complete examples. Search by name or task, or start with the choice cards below.
| Need | Start with | Check |
|---|---|---|
| Open a file | fopen() | Result is not NULL |
| Read a line | fgets() | Result and newline |
| Read blocks | fread() | Element count, feof, ferror |
| Format safely | snprintf() | Negative or at least capacity |
| Parse input | sscanf() | Exact assignment count |
Choose a Function by Job
Read a whole line?
Choose fgets(). It knows the array capacity and can preserve the newline.
Build formatted text?
Prefer snprintf() over sprintf() for bounded arrays.
Search All 45 Functions
Try a name or task such as line, binary, position, format, buffer, error, or temporary.
Open, close, and files
Compare the functions in this family by their exact stream contract and result check.
| Function | Best for | Tutorial |
|---|---|---|
fopen() | Opens a file and creates a C stream. | Open |
freopen() | Reassociates an existing stream with a file. | Open |
fclose() | Flushes buffered output and closes a stream. | Open |
remove() | Removes a file by name. | Open |
rename() | Renames or moves a file as supported by the host. | Open |
tmpfile() | Creates an automatically removed temporary file. | Open |
tmpnam() | Generates a temporary filename. | Open |
Character I/O
Compare the functions in this family by their exact stream contract and result check.
| Function | Best for | Tutorial |
|---|---|---|
fgetc() | Reads one character from a stream. | Open |
getc() | Reads one character, like fgetc. | Open |
getchar() | Reads one character from standard input. | Open |
fputc() | Writes one character to a stream. | Open |
putc() | Writes one character, like fputc. | Open |
putchar() | Writes one character to stdout. | Open |
ungetc() | Pushes one character back onto an input stream. | Open |
String I/O
Compare the functions in this family by their exact stream contract and result check.
Block I/O
Compare the functions in this family by their exact stream contract and result check.
Formatted output
Compare the functions in this family by their exact stream contract and result check.
| Function | Best for | Tutorial |
|---|---|---|
printf() | Writes formatted text to stdout. | Open |
fprintf() | Writes formatted text to a stream. | Open |
sprintf() | Formats text into an array without a size limit. | Open |
snprintf() | Formats bounded text into an array. | Open |
vprintf() | Writes formatted va_list output to stdout. | Open |
vfprintf() | Writes formatted va_list output to a stream. | Open |
vsprintf() | Formats va_list text without a size limit. | Open |
vsnprintf() | Formats bounded va_list text into an array. | Open |
Formatted input
Compare the functions in this family by their exact stream contract and result check.
| Function | Best for | Tutorial |
|---|---|---|
scanf() | Reads formatted values from stdin. | Open |
fscanf() | Reads formatted values from a stream. | Open |
sscanf() | Parses formatted values from a string. | Open |
vscanf() | Reads formatted stdin input through a va_list. | Open |
vfscanf() | Reads formatted stream input through a va_list. | Open |
vsscanf() | Parses a string through a va_list. | Open |
Position
Compare the functions in this family by their exact stream contract and result check.
Buffering and errors
Compare the functions in this family by their exact stream contract and result check.
| Function | Best for | Tutorial |
|---|---|---|
fflush() | Writes buffered output to its destination. | Open |
setbuf() | Selects full buffering or no buffering. | Open |
setvbuf() | Configures a stream's buffering mode and size. | Open |
clearerr() | Removes the EOF and error indicators from a stream. | Open |
feof() | Tests a stream's end-of-file indicator. | Open |
ferror() | Tests a stream's error indicator. | Open |
perror() | Prints a message for the current errno value. | Open |
Frequently Asked Questions
Reliable stdio Workflow
- Choose the stream and mode. Decide whether data is text or binary and whether the operation reads, writes, appends, or updates.
- Check acquisition. A file-opening function returning NULL means no stream exists to use or close.
- Check the operation itself. Character, element, assignment, and formatted-output counts have different meanings.
- Diagnose only after failure. EOF and stream error indicators explain a failed operation; they do not predict one.
- Finish deliberately. Flush where required and check fclose() when delayed output errors matter.
Start with clearerr()
Begin the complete stdio.h function sequence.
18 people found this page helpful
