Output with printf in C
printf writes formatted text to standard output: a format string with embedded conversion specifiers (%d, %s, %f) plus one matching argument per specifier. Unlike C++ streams there is no type safety net — a mismatched specifier is undefined behavior, which is exactly why -Wall -Wformat exists.
printf returns the character count (or negative on error), and output is line-buffered to terminals: text appears on newlines, while redirected output flushes at exit or via fflush. Precision, width, and flags live inside the specifier (%08.2f), covered fully in the format-specifiers lesson.
Format plus arguments
printf("%d items\n", n); — each % specifier consumes exactly one following argument, left to right.
Match types exactly
%d for int, %s for strings, %f for doubles (floats promote), %p for pointers — mismatches are undefined behavior, not just ugly output.
Newlines flush terminals
Ending lines with \n flushes interactive output; redirected streams buffer until full, flushed, or exited.
Check the return
printf returns characters written — negative signals a failed stream, worth checking in robust CLI tools.
#include <stdio.h> int main(void) { char name[] = "Ada"; int score = 95; printf("%s scored %d\n", name, score); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.