“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute every day to remember the basics of C language.
“Essential Knowledge Points for C Language Beginners: Series of 100 Notes“
15. printf() Function: Introduction to Formatting Output
1. Basic Usage of printf Function
printf is the most commonly used output function in the C standard library, declared in the stdio.h header file:
int printf(const char *format, ...);
Basic Example
#include <stdio.h>
int main() {
printf("Hello, World!\n"); // Output string
int age = 25;
printf("Age: %d\n", age); // Format output variable
return 0;
}
2. Detailed Explanation of Format Specifiers
printf controls the output format through format specifiers, with the basic syntax:
%[flags][width][.precision][length]type
Common Type Specifiers
| Specifier | Usage | Example |
| %d | Decimal integer | printf(“%d”, 123) |
| %f | Floating-point number | printf(“%f”, 3.14) |
| %c | Single character | printf(“%c”, ‘A’) |
| %s | String | printf(“%s”, “hello”) |
| %p | Pointer address | printf(“%p”, &var) |
| %% | Percent character | printf(“%%”) |
3. Formatting Control Parameters
1. Width Control
printf("%5d\n", 123); // " 123" (right-aligned, width 5)
printf("%-5d\n", 123); // "123 " (left-aligned)
2. Precision Control
printf("%.2f\n", 3.14159); // "3.14" (keep 2 decimal places)
printf("%.5s\n", "hello world"); // "hello" (truncate to 5 characters)
3. Flag Characters
printf("%+d\n", 123); // "+123" (show sign)
printf("%#x\n", 15); // "0xf" (show base prefix)
printf("%08d\n", 123); // "00000123" (leading zeros)
4. Special Format Output
1. Escape Characters
printf("Line1\nLine2"); // New line
printf("Tab\tColumn"); // Tab character
printf("Backslash: \\ "); // Output backslash
2. Multiple Parameter Output
int a = 10, b = 20;
printf("Values: %d, %d\n", a, b);
3. Dynamic Width/Precision
int width = 8, precision = 3;
printf("%*.*f\n", width, precision, 3.14159); // " 3.142"
5. Type Modifiers
| Modifier | Description | Example |
| h | short type | %hd (short int) |
| l | long type | %ld (long int) |
| ll | long long type | %lld (long long) |
| L | long double type | %Lf |
long bigNum = 1234567890L;
printf("%ld\n", bigNum);
6. Common Error Examples
1. Type Mismatch
float f = 3.14;
printf("%d\n", f); // Error! Should use %f
2. Insufficient Parameters
printf("%d %d\n", 1); // Missing second parameter
3. Format String Vulnerability
char user_input[100];
scanf("%s", user_input);
printf(user_input); // Dangerous! Should use printf("%s", user_input);
7. Advanced Usage Examples
1. Table Output
printf("%-10s %-10s %-10s\n", "Name", "Age", "Score");
printf("%-10s %-10d %-10.2f\n", "Alice", 20, 89.5);
printf("%-10s %-10d %-10.2f\n", "Bob", 22, 92.3);
2. Memory Address Output
int var = 10;
printf("Address: %p\n", (void*)&var);
3. Format Reuse
const char *fmt = "Result: %.*f\n";
printf(fmt, 2, 3.14159); // "Result: 3.14"
printf(fmt, 4, 2.71828); // "Result: 2.7183"
8. Return Value Explanation
printf returns the number of characters successfully output:
int count = printf("Hello");
// count = 5 (not including '\0')
9. Best Practice Recommendations
- 1. Safety Check:
if (printf("Test") < 0) { perror("printf failed"); } - 2. Avoid Buffer Overflow:
char buf[100]; snprintf(buf, sizeof(buf), "Value: %d", large_num);
———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to consume, the views are for learning reference only~~]

“If you like C, please like it”
Click the bottom right corner to view
“