stdio
<stdio.h> is the C library for formatted I/O and FILE streams. printf, scanf, fopen, and the stdin / stdout / stderr handles all live here.
Detailed Explanation
A FILE is an opaque buffered stream. Three are opened for you: stdin, stdout, and stderr. stdout is block-buffered when it points at a file and line-buffered when it points at a terminal. stderr is not fully buffered, so diagnostics appear even if the program dies before a flush. fflush(stdout) forces the buffer out. A newline on a terminal-associated stdout usually does too. A newline into a redirected file does not.
printf writes to stdout. fprintf writes to a FILE * you choose. sprintf writes to a character array with no bound and should be treated as a defect. snprintf(buf, buf_sz, fmt, ...) writes at most buf_sz bytes including the terminator and returns the length that would have been written. Check that return: a negative value is an encoding error, a value >= buf_sz means truncation.
The format string is a tiny language. %d is int, %u is unsigned, %ld is long, %zu is size_t, %s is char * to a terminator, %p is void *. A mismatch is undefined behavior. Compilers check literals when you enable -Wformat. They cannot check a format that arrived at run time. Never pass a user string as the format; use printf("%s", user).
scanf is harder to use correctly than it looks. %s has no bound and will overflow. %d leaves non-digits in the input. End of file, a matching failure, and a successful conversion all have to be distinguished via the return value. Many programs read lines with fgets and parse with sscanf or a hand-written converter, which at least bounds the first step.
Return values are the error channel. printf returns the number of characters written or a negative number. puts returns EOF on error. Ignoring those returns is how a full disk looks like a successful run. Check them on the paths that matter, and check ferror / fclose at the end.
Code Examples
#include <stdio.h>
int main(void)
{
const char *name = "c";
int n = printf("hello, %s\n", name); /* prints: hello, c */
if (n < 0) {
return 1;
}
char buf[8];
int need = snprintf(buf, sizeof buf, "count=%d", 42);
if (need < 0 || (size_t)need >= sizeof buf) {
fprintf(stderr, "truncated or failed, need=%d\n", need);
return 1;
}
printf("%s\n", buf); /* prints: count=42 */
return 0;
}#include <stdio.h>
int main(void)
{
char line[32];
if (fgets(line, sizeof line, stdin) == NULL) {
if (ferror(stdin)) {
perror("stdin");
return 1;
}
return 0; /* EOF with no data */
}
printf("read: %s", line);
return 0;
}Common Gotchas
A user-controlled format string is a vulnerability
`printf(user)` lets the caller read the stack with `%x` and write it with `%n`. Always use `printf("%s", user)`.
`%d` for `size_t` or a pointer is undefined behavior
use `%zu` and `%p`. Enable `-Wformat`.
`stdout` is buffered
a crash after `printf` without a newline may print nothing. Flush, or write diagnostics to `stderr`.
`scanf("%s", buf)` is an unbounded write
use `fgets` or a width like `%31s` that matches the array.