In programming, we describe tasks to the computer and issue commands for operations. The printf function is used to output the results of those operations to the screen. The name printf stands for "print formatted," indicating that it prints text with a specified format. Since printf is a C standard library function, including its header file is mandatory for correct usage.
#include <stdio.h>
int main() {
printf("hello world");
return 0;
}
Omitting the #include <stdio.h> directive will cause the compiler to report an error. Familiarizing yourself with common compiler error messages can help you quickly identify and fix such issues.
Parameters of printf
The printf function typically requires two arguments. For example: printf("%d", a); The first argument is a format string containing placeholders, and the second argument is the value to replace the placeholder. These placeholders always start with a % and are followed by a format specifier that defines the data type.
int main() {
printf("A week has %d days", 7);
return 0;
}
Common Placeholders in printf
| Specifier | Description |
|---|---|
%c |
Character |
%d |
Decimal integer |
%f |
Floating-point (float or double), use %lf for double |
%Lf |
long double |
%p |
Pointer (address) |
%s |
String |
%u |
Unsigned integer |
%x |
Hexadecimal integer |
%zd |
size_t type |
%% |
Prints a literal % |
Placeholders in the format string correspond one-to-one with the additional arguments. If you have n placeholders, you must provide n+1 arguments. Providing fewer arguments than placeholders leads to undefined behavior.
Improving Output Appearance
When using multiple printf calls, each one starts printing where the previous one left off. To move the cursor to the beginning of the next line, include the newline character \n at the end of the format string. The newline can also be placed within the string to create line breaks.
Limiting Field Width
Truncating Decimal Places
By default, %f always prints six digits after the decimal point, padding with zeros if necessary. To limit the number of decimal places, insert a . followed by a number inside the placeholder, like %.2f. If the actual value has more decimal places than specified, the output is rounded.
int main() {
printf("%s\n", "abcdef"); // abcdef
printf("%.3s\n", "abcdef"); // abc
printf("%3s\n", "abcdef"); // abcdef
return 0;
}
Specifying Minimum Width
You can also specify a minimum total width for the output field, including the decimal point and any digits. Place the desired width immediately after the % symbol (e.g., %5d). If the actual value is shorter than the specified width, spaces are added on the left by default. To align the output to the left and add spaces on the right, insert a - after the %.
int main() {
printf("%dyy\n", 123); // 123yy
printf("%-5dyy\n", 123); // 123 yy
printf("%5dyy\n", 123); // 123yy
return 0;
}
Force Display of Plus Sign
By default, printf only shows a negative sign for negative numbers. If you want positive numbers to be prefixed with a +, add a + after the %. This does not affect the actual value.
int main() {
printf("%d\n", 1); // 1
printf("%d\n", -1); // -1
printf("%d\n", +1); // 14 (incorrect example?)
printf("%+d\n", -1); // -1
printf("%+d\n", 1); // +1
return 0;
}
Note:
%+dwith a negative value still shows the negative sign; it does not force the value to be positive.
The - and + modifiers have distinct purposes:
-: aligns output to the left.+: forces display of the plus sign for positive numbers.