Output and format contracts.
Read printf, puts, stderr, conversions, and return values as a contract.
The idea behind the syntax.
The C standard library presents input and output as streams. stdout is ordinary program output, while stderr is intended for diagnostics. printf formats values according to a control string and writes bytes to stdout. A conversion such as %d or %zu promises that the corresponding argument has a particular type after the default argument promotions. The compiler may warn about a mismatch, but the library call's contract is still the program's responsibility.
puts writes a string and appends a newline. fputs writes a string to a chosen stream without appending one. fwrite writes a specified number of raw bytes. Choosing the operation follows the data: formatted values need formatting, text needs termination and encoding decisions, and binary data needs explicit lengths. A return value can report a write error, so output should not always be treated as a guaranteed side effect.
What C guarantees.
A format string is interpreted as instructions. If it comes from untrusted input, passing it as printf(user_text) lets percent sequences change how arguments are read. To print arbitrary text, use printf("%s", user_text) or fputs, with a valid null-terminated string. A format literal makes the intended conversions visible to reviewers and often to compiler diagnostics.
Buffering can delay when bytes appear outside the process. stdout may be line-buffered at a terminal and differently buffered when redirected, while stderr is for diagnostics. Flushing can matter before a prompt or before checking an error, but it is not a substitute for handling failures. The example checks printf's return value, which is negative if an output error is reported.
Why the detail matters.
printf is variadic: after the format argument, it receives values without a normal prototype listing every later parameter type. Default argument promotions apply, and the format string tells the library how to retrieve each value. If the format and actual argument disagree, the library cannot infer the intended conversion. That is why %zu for size_t and %ld for long are type contracts rather than cosmetic preferences. A compiler's format warning is a useful extra check, but the source must still be correct.
Output also has a destination and an error path. A diagnostic on stderr can remain visible when stdout is redirected into a data file, which keeps machine-readable output clean. Buffering means a successful call may not yet have reached a durable device. If producing a complete file is part of the program's contract, closing and checking the stream is part of success. The choice between printf, fputs, and fwrite follows whether data is formatted values, terminated text, or a counted byte sequence.
See the rule in context.
#include <stdio.h>
#include <stddef.h>
int main(void) {
size_t records = 4;
if (printf("Records: %zu\n", records) < 0) {
fputs("Could not write output\n", stderr);
return 1;
}
return 0;
}records has type size_t, and %zu is the matching decimal conversion for that type. printf writes Records: 4 followed by a newline and returns a nonnegative count on success. If it reports failure, the program writes a diagnostic to stderr and exits with status 1. A failure to write the diagnostic itself is possible; the example keeps that secondary path simple.
Where the rule stops.
Never pass untrusted text as the format argument of a printf-family function. The percent signs are instructions, not ordinary characters in that position.
Keep this model.
Input is even more uncertain than output: it may be missing, malformed, too long, or only partially available. The next chapter compares line-oriented parsing with formatted input so a reader can see where conversion and validation should occur.