printf()
Prints formatted output to stdout.
printf("format string", arguments...);This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Prints formatted output to stdout.
The printf (print formatted) function, declared in <stdio.h>, is the standard C library function for sending formatted output to stdout. It operates by parsing a format string containing conversion specifiers (e.g., %d, %f, %s) and mapping them to a variadic list of arguments. From a performance perspective, printf is relatively expensive compared to write() or puts() because it must parse the format string character-by-character at runtime. Regarding thread safety, it is generally thread-safe in POSIX environments due to internal locking of the FILE stream. Edge cases include the use of the %n specifier, which writes the number of characters printed so far to an integer pointer, and the buffering behavior of stdout—which is typically line-buffered, meaning output may be delayed until a newline character is encountered or fflush() is invoked. It returns the total count of characters printed, which can be used to track output alignment or detect write errors.
Quick reference
Syntax
printf("format string", arguments...);
Inputs
Parameters
See it in practice
Examples
Basic Type Formatting
int score = 95;
float average = 88.5;
char grade = 'A';
printf("Score: %d, Avg: %f, Grade: %c\n", score, average, grade);Score: 95, Avg: 88.500000, Grade: A
Demonstrates the usage of standard integer, floating-point, and character specifiers.
Width and Precision Control
double value = 12.345678;
int id = 7;
printf("ID: %04d | Value: %.2f\n", id, value);ID: 0007 | Value: 12.35
Uses field width and precision to pad numbers with leading zeros and round decimals.
Handling the Return Value
const char *msg = "Success";
int length = printf("%s", msg);
printf("\nTotal chars: %d\n", length);Success Total chars: 7
Captures the integer return value representing the number of characters written.
Debug faster
Common Errors
TypeError
Cause: Passing a variable that does not match the expected type for the format specifier, such as passing a double to %d.
Fix: Ensure the specifier matches the argument type: %d for int, %f for float/double, %s for char*, and %p for pointers.
double pi = 3.14; printf("%d", pi); // Garbage value or crashLogicError
Cause: Format string vulnerability caused by passing a user-controlled string directly as the first argument.
Fix: Always use a literal format string with a specifier when printing variable content.
char user_input[50]; scanf("%s", user_input); printf(user_input); // Unsafe: use printf("%s", user_input) insteadRuntime support
Compatibility
Part of stdio.h standard library
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Prints formatted output to stdout.
format: Format string with specifiers. ...: Values to format and print.
TypeError: Ensure the specifier matches the argument type: %d for int, %f for float/double, %s for char*, and %p for pointers. LogicError: Always use a literal format string with a specifier when printing variable content.