printf() / fprintf() / sprintf()
Formats and outputs values using a format string. printf() writes to standard output (the screen), while fprintf() writes to a specified stream (such as a file or standard error). Both are declared in <stdio.h>.
Syntax
#include <stdio.h> // Writes formatted output to standard output. printf(format_string, arg1, arg2, ...); // Writes formatted output to the specified stream. fprintf(stream, format_string, arg1, arg2, ...); // Writes formatted output to a string buffer. sprintf(buffer, format_string, arg1, arg2, ...); // Safe version — also takes a buffer size limit. snprintf(buffer, size, format_string, arg1, arg2, ...);
Common Format Specifiers
| Specifier | Type | Description |
|---|---|---|
| %d / %i | int | Outputs an integer as a decimal number. |
| %u | unsigned int | Outputs an unsigned decimal integer. |
| %f | float / double | Outputs a floating-point number with a decimal point. Defaults to 6 decimal places. |
| %e / %E | float / double | Outputs a number in scientific notation (e.g., 1.23e+02). |
| %g / %G | float / double | Outputs using whichever of %f or %e produces the shorter result. |
| %c | char | Outputs a single character. |
| %s | char * | Outputs a string up to the null terminator. |
| %p | void * | Outputs a pointer address in hexadecimal. |
| %x / %X | unsigned int | Outputs an integer in hexadecimal. |
| %o | unsigned int | Outputs an integer in octal. |
| %zu | size_t | Outputs an unsigned size type, such as the return value of sizeof. |
| %% | — | Outputs a literal % character. |
Sample Code
#include <stdio.h>
int main(void) {
// Examples of basic format specifiers.
int n = 42;
double d = 3.14159;
char c = 'A';
char *s = "Hello";
printf("Integer: %d\n", n); // Outputs: "Integer: 42"
printf("Float: %f\n", d); // Outputs: "Float: 3.141590"
printf("Char: %c\n", c); // Outputs: "Char: A"
printf("String: %s\n", s); // Outputs: "String: Hello"
// Flags and width specifiers.
printf("[%10d]\n", n); // Right-aligned (width 10): "[ 42]"
printf("[%-10d]\n", n); // Left-aligned (width 10): "[42 ]"
printf("[%010d]\n", n); // Zero-padded (width 10): "[0000000042]"
// Precision specifiers.
printf("%.2f\n", d); // 2 decimal places: "3.14"
printf("%.5s\n", s); // Truncate string to 5 chars: "Hello"
printf("%8.2f\n", d); // Width 8, 2 decimal places: " 3.14"
// Use fprintf to write an error message to standard error.
fprintf(stderr, "An error occurred.\n");
// Use snprintf to write formatted output into a string buffer.
char buf[64];
snprintf(buf, sizeof(buf), "Score: %d points", 98);
printf("%s\n", buf); // Outputs: "Score: 98 points"
// Hexadecimal and octal output.
printf("Hex (lower): %x\n", 255); // Outputs: "ff"
printf("Hex (upper): %X\n", 255); // Outputs: "FF"
printf("Octal: %o\n", 255); // Outputs: "377"
printf("0x prefix: %#x\n", 255); // Outputs: "0xff"
return 0;
}
Notes
The general format of a format specifier is %[flags][width][.precision]type. Available flags include left-align (-), zero-pad (0), and always show sign (+).
If a format specifier does not match the type of the corresponding argument, undefined behavior occurs. For example, using %d with a floating-point value will produce incorrect output. Enable compiler warnings (e.g., the -Wall option in gcc) to catch type mismatches.
For formatted input (reading values with a format string), see scanf() / fscanf(). For character-by-character I/O, see putchar() / getchar().
If you find any errors or copyright issues, please contact us.