C stdio.h Functions

Beginner
16 min read
45 tutorials
<stdio.h>

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.

NeedStart withCheck
Open a filefopen()Result is not NULL
Read a linefgets()Result and newline
Read blocksfread()Element count, feof, ferror
Format safelysnprintf()Negative or at least capacity
Parse inputsscanf()Exact assignment count

Choose a Function by Job

Open or close?

Use fopen(), always check NULL, and finish with fclose().

Read a whole line?

Choose fgets(). It knows the array capacity and can preserve the newline.

Read one byte?

Choose fgetc() or getchar(), storing the result in int.

Transfer records?

Use fread() and fwrite(), checking complete element counts.

Build formatted text?

Prefer snprintf() over sprintf() for bounded arrays.

Explain a failed read?

After the read fails, inspect feof() and ferror().

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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
fgets()Reads a bounded line or character sequence.Open
fputs()Writes a string without adding a newline.Open
puts()Writes a string and a trailing newline to stdout.Open

Block I/O

Compare the functions in this family by their exact stream contract and result check.

FunctionBest forTutorial
fread()Reads blocks of binary or text data.Open
fwrite()Writes blocks of data to a stream.Open

Formatted output

Compare the functions in this family by their exact stream contract and result check.

FunctionBest forTutorial
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.

FunctionBest forTutorial
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.

FunctionBest forTutorial
fseek()Moves a stream's file-position indicator.Open
ftell()Reports a stream's current file position.Open
rewind()Returns a stream to its beginning.Open
fgetpos()Saves a stream position in an fpos_t object.Open
fsetpos()Restores a stream position saved by fgetpos.Open

Buffering and errors

Compare the functions in this family by their exact stream contract and result check.

FunctionBest forTutorial
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

It is the C standard input/output header for streams, files, formatted I/O, buffering, positioning, and status checks.
Use fgets() with an array capacity, then check whether a newline was stored.
Check the read result first, then use feof() and ferror() to distinguish normal end-of-file from an I/O error.
Prefer snprintf() for fixed arrays because it accepts the destination capacity and reports the required length.
It depends on the function: pointers, character values, assignment counts, element counts, or zero. Each tutorial states the success value and its distinct failure sentinel.

Reliable stdio Workflow

  1. Choose the stream and mode. Decide whether data is text or binary and whether the operation reads, writes, appends, or updates.
  2. Check acquisition. A file-opening function returning NULL means no stream exists to use or close.
  3. Check the operation itself. Character, element, assignment, and formatted-output counts have different meanings.
  4. Diagnose only after failure. EOF and stream error indicators explain a failed operation; they do not predict one.
  5. Finish deliberately. Flush where required and check fclose() when delayed output errors matter.

Start with clearerr()

Begin the complete stdio.h function sequence.

clearerr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful