<stdio.h> is the C standard input/output header. It gives you printf, scanf, file functions like fopen / fgets, and the streams stdin, stdout, and stderr.
Remember
#include <stdio.h>
printf / scanf → formatted console I/O
fopen / fclose → open and close files
fprintf / fgets → formatted / line I/O on FILE*
stdin → keyboard (default)
stdout → screen (default)
stderr → errors (unbuffered-ish)
FILE *fp; EOF means end / failure
always check fopen for NULL
fopen fails. Missing file in "r" mode returns NULL.
Check immediately. Never call fgets or fprintf on a null pointer.
perror explains. It prints your label plus the message from errno.
Exit with an error. Returning 1 signals failure to the shell.
Edge Cases & Pitfalls
Check these before shipping I/O code.
scanf
Always pass & for scalars
scanf("%d", age) without & is undefined behavior. Arrays already decay to pointers.
buffer
Never use gets()
gets cannot bound the buffer. Prefer fgets(buf, sizeof(buf), stdin).
null
Check fopen for NULL
Missing paths, permissions, or full disks return NULL. Call perror and stop.
eof
Store fgetc in an int
EOF is not a valid char value on all systems. Use int c = fgetc(fp);.
Analysis
Performance Notes
Approach
Cost
Best for
printf / fprintf
Formatting + buffered I/O
Human-readable text
fgets / line loops
Moderate; still buffered
Text files line by line
fread / fwrite
Fewer syscalls in bulk
Binary or large blocks
stdio buffers output for speed. Force a flush with fflush when a prompt must appear before the next read.
Remember
Key Takeaways
Role: formatted console and file I/O via FILE* streams.
Safety: check fopen, bound reads with fgets, pass & to scanf.
Streams:stdout for normal output, stderr for errors.
Errors:perror after failed opens explains errno.
One line:stdio.h is how C talks to the keyboard, screen, and files—always check opens and bound your buffers.
Frequently Asked Questions
stdio.h is the Standard Input/Output header. It declares printf, scanf, fopen, fclose, fgets, and related functions for console and file I/O. It also defines FILE, EOF, and the standard streams stdin, stdout, and stderr.
printf writes formatted text to stdout. fprintf takes a FILE* first, so you can write to a file or any stream: fprintf(fp, "%d\n", n) or fprintf(stderr, "error\n").
scanf stores into variables, so it needs addresses. For int x use scanf("%d", &x). Arrays decay to pointers, so scanf("%99s", name) does not need & on the array name.
EOF is a macro (usually -1) meaning end-of-file or no more input. fgetc returns EOF as an int. Do not store fgetc results in a char—use int so EOF is distinguishable.
perror prints your message, a colon, then a system error text based on errno. Call it after a failed fopen or similar call to see why it failed.
fflush forces buffered output to be written now. Call fflush(stdout) before a prompt without a newline, or fflush(fp) when you need data on disk before continuing.