C stdio.h Tutorial

Beginner
8 min read
<stdio.h>

What stdio.h Does

The C <stdio.h> header declares the standard input/output library. It gives programs a common way to read characters and lines, write text, format values, transfer blocks, open files, move through seekable files, and inspect stream status.

The central idea is a stream: an ordered flow of characters connected to a file, terminal, pipe, or another host resource. The library can buffer that flow for efficiency, so a successful write call may place data in memory before it reaches the final destination.

FILE*, stdin, stdout, and stderr

A FILE * is a handle managed by the C library. It records the current position, buffering state, end-of-file indicator, error indicator, and the connection to the underlying resource. Do not access its internals; pass it to stdio functions.

StreamDefault roleTypical use
stdinStandard inputfgets(line, sizeof line, stdin)
stdoutNormal outputprintf("count=%d\n", count)
stderrDiagnosticsfprintf(stderr, "failed\n")

These three streams are available when the program starts. Files opened by your program return additional FILE * values that should normally be closed with fclose().

Choose an fopen Mode Carefully

ModeMeaningIf file exists
"r"Read existing textKept; open starts at beginning
"w"Write textTruncated to empty
"a"Append textWrites go to the end
"r+"Read and write existing textKept
"w+"Read and write textTruncated
"rb"Read binaryKept

printf() vs puts()

Use printf() when text must include converted values, widths, precision, or several data types. Its format string is a contract: every conversion specification must match the corresponding argument type.

Use puts() for one already-built string when you want a trailing newline. It is simpler and avoids format-string interpretation. Use fputs(text, stdout) when you do not want an automatic newline.

GoalCall
Print a label and integerprintf("score=%d\n", score);
Print a complete lineputs("Ready");
Print without newlinefputs("Loading...", stdout);

Write and Read a Small File

This complete example opens a file for writing, checks every acquisition and close that can fail, then reopens the same file and reads one bounded line.

C
#include <stdio.h>

int main(void) {
    FILE *file = fopen("greeting.txt", "w");
    if (file == NULL) {
        perror("greeting.txt");
        return 1;
    }

    if (fprintf(file, "Hello, stdio!\n") < 0 ||
        fclose(file) == EOF) {
        perror("write");
        return 1;
    }

    file = fopen("greeting.txt", "r");
    if (file == NULL) {
        perror("greeting.txt");
        return 1;
    }

    char line[64];
    if (fgets(line, sizeof line, file) != NULL)
        printf("Read back: %s", line);
    else if (ferror(file))
        perror("read");

    fclose(file);
    return 0;
}

How It Works

  1. Open for writing. Mode w creates the file or truncates it, so the NULL check happens immediately.
  2. Write and close. fprintf() reports formatted-output failure and fclose() can reveal a delayed buffered-write failure.
  3. Open for reading. The old stream is closed, and a new FILE pointer starts at the beginning.
  4. Read within bounds. fgets() receives the complete array capacity and the program diagnoses an actual stream error separately.

Where to Go Next

Open the searchable function catalog to compare all 45 functions by task. A useful beginner sequence is fopen(), fclose(), fgets(), fputs(), printf(), fread(), fwrite(), feof(), and ferror().

Browse all stdio.h functions

Choose by job or search the complete tutorial catalog.

Function catalog →

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.

14 people found this page helpful