Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →In C, printf writes formatted output to standard output, usually the console. Include <stdio.h>, put conversion specifications such as %s or %d in a format string, and supply matching arguments in the same order. Use snprintf instead when you need to build text in a bounded character array.
What printf does
The C standard-library function printf writes formatted text to stdout. Characters in its format string are copied to the output, while each conversion specification beginning with % formats a corresponding argument. The function is declared in <stdio.h>.
For example:
#include <stdio.h>
int main(void) {
const char *name = "Ada";
int count = 3;
printf("%s has %d itemsn", name, count);
return 0;
}
Here, %s formats the character string pointed to by name, %d formats the integer count, and n is a newline escape in the C string literal. The format string and arguments form a contract: each conversion must have a corresponding argument of the required type, taking default argument promotions into account.
Common printf conversion specifiers
| Specifier | Use | Example argument |
|---|---|---|
%d, %i |
Signed decimal integer | int |
%u |
Unsigned decimal integer | unsigned int |
%o |
Unsigned octal integer | unsigned int |
%x, %X |
Unsigned hexadecimal integer, lowercase or uppercase digits | unsigned int |
%f |
Floating-point notation | double |
%e, %E |
Scientific notation | double |
%g, %G |
Chooses a compact floating-point representation | double |
%c |
Character | int value representing a character |
%s |
Null-terminated character string | Pointer to a character array |
%p |
Pointer representation | Pointer value; the printed form is implementation-defined |
%% |
Literal percent sign; consumes no argument | None |
These are the common conversion families. Newer C versions, POSIX, and individual runtimes may provide additional conversions or extensions, so check the documentation for the C version and target implementation when relying on anything beyond the portable set.
#1 Best Overall
How to read a format specification
A conversion specification can contain flags, a field width, a precision, a length modifier, and a conversion character. The conversion character determines the kind of value being formatted. The other parts adjust its presentation or specify how an argument is interpreted.
Width sets a minimum, not a maximum
A field width reserves at least that many characters for a conversion. If the formatted value needs more space, it is not cut off just because a width was specified. Use precision, where the conversion supports it, to control details such as the digits after a floating-point radix character or the maximum string output under the applicable string rules.
Precision depends on the conversion
Precision does not have one universal meaning. For floating-point conversions it can control the number of digits after the radix character; for strings it can limit the number of bytes or characters printed according to the applicable rules. Consult the conversion’s specification rather than assuming that width or precision behaves identically for every type.
Choose the function for the destination
| Need | Function | How it differs |
|---|---|---|
| Write to standard output | printf |
Formats output for stdout. |
| Write to a selected stream | fprintf |
Formats output for a supplied FILE stream, such as a file. |
| Format into a bounded character array | snprintf |
Accepts the destination capacity, helping prevent writing beyond the array. |
Write through a variadic wrapper that already has a va_list |
vprintf, vfprintf, or vsnprintf |
Provides the corresponding formatted-output functionality for a va_list. |
Use snprintf for bounded string construction
Pass snprintf the full capacity of the destination array, including space for the terminating null character. Its return value is the number of characters that would have been produced, excluding that terminator. A nonnegative return value greater than or equal to the supplied capacity means the output was truncated; a negative return indicates an error.
char message[32];
int needed = snprintf(message, sizeof message, "%s: %d", "Items", 3);
if (needed < 0) {
/* Handle a formatting or encoding error. */
} else if ((size_t)needed >= sizeof message) {
/* The complete output did not fit. */
}
Do not substitute sprintf for bounded formatting: it has no destination-size parameter. The formatted output must also not overlap its source and destination in ways prohibited by the function’s specification.
Match arguments to conversions
Because printf is variadic, it does not receive a type description for each extra argument at runtime. Supplying too few arguments or an argument with the wrong type for a conversion causes undefined behavior. A format string that looks plausible can therefore still produce incorrect output or worse.
- Check every conversion against the type of its corresponding argument.
- Remember that arguments undergo default argument promotions when passed to a variadic function.
- Do not assume a conversion will automatically adapt to whatever type was supplied.
GCC can check format usage for standard functions when the relevant diagnostics are enabled, and its format attributes can enable checking for custom printf-like functions. These warnings are useful, but they do not replace correct format/type pairing. See the GCC 13.3.0 warning options documentation.
Keep user input out of the format string
A format string is instructions, not merely text. Never pass untrusted input as the format argument, as in printf(user_input): percent sequences in that input may be interpreted as conversions. To print user-provided text as data, use a fixed format such as printf("%s", user_input).
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Best Value
The %n conversion is especially sensitive: instead of printing text, it writes the number of characters output so far through a pointer argument. Do not let untrusted format strings control whether this conversion is used.
Locale, portability, and output errors
Locale can change numeric presentation
Numeric formatting may depend on LC_NUMERIC. For example, the Linux man-pages documentation describes locales in which the radix character is a comma rather than a period and grouping separators appear. If another program must parse the output, do not assume a locale-formatted number will always use . or the same grouping convention. See Linux man-pages: printf(3).
Extensions are not automatically portable
Linux documentation describes positional arguments such as %2$d, which can select an argument by position, but this $ syntax is not part of C99. POSIX additions and runtime-specific conversions likewise should not be assumed to work on every C implementation. For portable code, follow the relevant C standard and verify any platform-specific feature in the target’s documentation.
Check return values when output matters
printf returns the number of characters transmitted, or a negative value if an output or encoding error occurs. Check that return value when the program needs to detect failures; for buffer formatting, use snprintf‘s return value to detect errors and determine whether the output fit.
Free tools Windows power users keep installed
One-click scans. No signup required.
Do not confuse C’s function with the shell command
This article concerns the C library function called from a C program. GNU Coreutils also provides a shell utility named printf, and shells may provide their own built-in command. Their syntax and behavior are not a substitute for the C function’s interface. The GNU Coreutils 9.11 manual documents the command-line utility.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

