Learn C
#include "ioformatting/printf.h"

Output with printf in C

#concept

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.

#how it works
1

Format plus arguments

printf("%d items\n", n); — each % specifier consumes exactly one following argument, left to right.

2

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.

3

Newlines flush terminals

Ending lines with \n flushes interactive output; redirected streams buffer until full, flushed, or exited.

4

Check the return

printf returns characters written — negative signals a failed stream, worth checking in robust CLI tools.

#example
#include <stdio.h>
 
int main(void) {
char name[] = "Ada";
int score = 95;
printf("%s scored %d\n", name, score);
return 0;
}
Output
Ada scored 95
1%s consumes the name array's characters, %d consumes the int — order matches arguments left to right.
2The trailing \n in the format ends the line and flushes terminal output.
3One call produced the full composed line — format strings assemble mixed output declaratively.
#use cases
Every console message, table row, and debug dump in C
Composed output mixing strings, ints, and floats in one call
Checking printf's return in tools where failed output matters
Letting -Wformat verify specifier/argument agreement at compile time

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.