C stdio.h Tutorial

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.
| Stream | Default role | Typical use |
|---|---|---|
stdin | Standard input | fgets(line, sizeof line, stdin) |
stdout | Normal output | printf("count=%d\n", count) |
stderr | Diagnostics | fprintf(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
| Mode | Meaning | If file exists |
|---|---|---|
"r" | Read existing text | Kept; open starts at beginning |
"w" | Write text | Truncated to empty |
"a" | Append text | Writes go to the end |
"r+" | Read and write existing text | Kept |
"w+" | Read and write text | Truncated |
"rb" | Read binary | Kept |
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.
| Goal | Call |
|---|---|
| Print a label and integer | printf("score=%d\n", score); |
| Print a complete line | puts("Ready"); |
| Print without newline | fputs("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.
#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
- Open for writing. Mode w creates the file or truncates it, so the NULL check happens immediately.
- Write and close. fprintf() reports formatted-output failure and fclose() can reveal a delayed buffered-write failure.
- Open for reading. The old stream is closed, and a new FILE pointer starts at the beginning.
- 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.
14 people found this page helpful
