C Notes
Complete guide to printf() in C: format specifiers, width, precision, flags, escape sequences, and practical formatting examples. Master formatted output in C programming.
The printf() function is your primary tool for displaying output to the console in C. It stands for "print formatted" and lives in the <stdio.h> header. What makes printf() powerful is its format string — a mini-language that gives you precise control over how numbers, characters, and strings appear on screen.
Basic Syntax
int printf(const char *format, ...);The function takes a format string followed by optional arguments. It returns the number of characters printed, or a negative value on error.
#include <stdio.h>
int main() {
// Simple text
printf("Hello, World!\n");
// With format specifiers
printf("I am %d years old\n", 25);
printf("Pi is approximately %f\n", 3.14159);
printf("My name is %s\n", "Alice");
// Return value
int chars_printed = printf("Hello\n");
printf("Previous printf printed %d characters\n", chars_printed);
return 0;
}Hello, World! I am 25 years old Pi is approximately 3.141590 My name is Alice Hello Previous printf printed 6 characters
Format Specifiers
Format specifiers begin with % and tell printf() how to interpret and display the corresponding argument:
| Specifier | Data Type | Example Output |
|---|---|---|
%d or %i | int (signed decimal) | 42, -7 |
%u | unsigned int | 42 |
%f | float/double | 3.141590 |
%e or %E | Scientific notation | 3.141590e+00 |
%g or %G | Shorter of %f or %e | 3.14159 |
%c | Character | A |
%s | String | Hello |
%p | Pointer address | 0x7ffd5e8a |
%x or %X | Hexadecimal | 1a or 1A |
%o | Octal | 52 |
%% | Literal % | % |
%ld | long int | 1234567890 |
%lld | long long int | 9876543210 |
%lf | double (in scanf) | 3.14 |
#include <stdio.h>
int main() {
int num = 255;
float pi = 3.14159;
char ch = 'Z';
char name[] = "World";
printf("Decimal: %d\n", num);
printf("Unsigned: %u\n", num);
printf("Octal: %o\n", num);
printf("Hex (lower): %x\n", num);
printf("Hex (upper): %X\n", num);
printf("Float: %f\n", pi);
printf("Scientific: %e\n", pi);
printf("General: %g\n", pi);
printf("Character: %c\n", ch);
printf("String: %s\n", name);
printf("Pointer: %p\n", (void*)&num);
printf("Percent sign: 100%%\n");
return 0;
}Decimal: 255 Unsigned: 255 Octal: 377 Hex (lower): ff Hex (upper): FF Float: 3.141590 Scientific: 3.141590e+00 General: 3.14159 Character: Z String: World Pointer: 0x7ffd5e8a3c4c Percent sign: 100%
Width and Precision
The complete format specifier syntax is:
Width — Minimum Field Width
Width specifies the minimum number of characters to output. If the value is shorter, it's padded with spaces (by default, right-aligned):
[ 42] [ 12345] [ Hi] [ Hello!] [42 ] [Hi ]
Precision
For floats, precision controls decimal places. For strings, it limits characters:
Default: 3.141593 2 decimals: 3.14 4 decimals: 3.1416 0 decimals: 3 Full string: Hello, World! 5 chars: Hello 3 chars: Hel [ 3.14] [3.14 ]
Flags
Flags modify the output format:
| Flag | Description | Example |
|---|---|---|
- | Left-align within width | %-10d |
+ | Show + for positive numbers | %+d |
(space) | Space before positive numbers | % d |
0 | Pad with zeros instead of spaces | %05d |
# | Alternate form (0x for hex, etc.) | %#x |
Plus flag: +42, -42 Space flag: 42, -42 Zero-pad: 00042 Zero-pad: 00003.14 Hex: ff vs 0xff Octal: 10 vs 010 [+42 ] [0000000042]
Escape Sequences
Special characters in format strings:
| Sequence | Meaning |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\0 | Null character |
\a | Alert (bell) |
\r | Carriage return |
#include <stdio.h>
int main() {
printf("Line 1\nLine 2\n");
printf("Col1\tCol2\tCol3\n");
printf("He said \"Hello\"\n");
printf("Path: C:\\Users\\Admin\n");
printf("100%% complete\n");
return 0;
}Line 1 Line 2 Col1 Col2 Col3 He said "Hello" Path: C:\Users\Admin 100% complete
Printing Different Data Types
#include <stdio.h>
int main() {
// Integer types
short s = 32767;
int i = 2147483647;
long l = 2147483647L;
long long ll = 9223372036854775807LL;
printf("short: %hd\n", s);
printf("int: %d\n", i);
printf("long: %ld\n", l);
printf("long long: %lld\n", ll);
// Unsigned types
unsigned int ui = 4294967295U;
printf("unsigned: %u\n", ui);
// Floating-point types
float f = 3.14f;
double d = 3.141592653589793;
printf("float: %f\n", f);
printf("double: %lf\n", d);
printf("double (e): %e\n", d);
// Size
printf("sizeof(int): %zu bytes\n", sizeof(int));
return 0;
}short: 32767 int: 2147483647 long: 2147483647 long long: 9223372036854775807 unsigned: 4294967295 float: 3.140000 double: 3.141593 double (e): 3.141593e+00 sizeof(int): 4 bytes
Practical Formatting Examples
Table Output
#include <stdio.h>
int main() {
printf("%-15s %8s %10s\n", "Product", "Qty", "Price");
printf("%-15s %8s %10s\n", "-------", "---", "-----");
printf("%-15s %8d %10.2f\n", "Laptop", 2, 999.99);
printf("%-15s %8d %10.2f\n", "Mouse", 5, 19.99);
printf("%-15s %8d %10.2f\n", "Keyboard", 3, 49.50);
printf("%-15s %8s %10s\n", "", "", "----------");
printf("%-15s %8s %10.2f\n", "Total", "", 2249.45);
return 0;
}Product Qty Price
------- --- -----
Laptop 2 999.99
Mouse 5 19.99
Keyboard 3 49.50
----------
Total 2249.45Progress Bar
[==================================================>] 100%
Common Mistakes
#include <stdio.h>
int main() {
// Mismatch: using %d for float
float f = 3.14;
printf("Wrong: %d\n", f); // Undefined! Use %f
printf("Right: %f\n", f);
// Forgetting arguments
// printf("%d %d\n", 5); // Missing second argument - undefined behavior
// Using %d for long long
long long big = 9876543210LL;
printf("Wrong: %d\n", big); // Truncated!
printf("Right: %lld\n", big);
return 0;
}Interview Questions
Q1: What is the difference between %d and %i in printf()?
In printf(), %d and %i are identical — both print a signed integer in decimal. The difference only matters in scanf(): %d reads only decimal, while %i reads decimal, octal (if prefixed with 0), or hexadecimal (if prefixed with 0x).
Q2: What does printf() return?
printf() returns the total number of characters successfully printed. On error, it returns a negative value. For example, printf("Hello\n") returns 6 (5 characters + newline).
Q3: What happens if the format specifier doesn't match the argument type?
It's undefined behavior. For example, using %d with a float argument may print garbage because printf() interprets the bit pattern of the float as an integer. Always ensure format specifiers match argument types.
Q4: How do you print a literal % sign using printf?
Use %%. A single % is interpreted as the start of a format specifier, so you must escape it by doubling it: printf("100%%") prints 100%.
Q5: What is the difference between %f and %lf in printf?
In printf(), both %f and %lf print a double (since floats are promoted to double when passed to variadic functions). The l modifier is ignored in printf. However, in scanf(), %f reads a float while %lf reads a double — this distinction is critical.
Summary
printf()formats and prints data to the console using format specifiers- The format string syntax is
%[flags][width][.precision][length]specifier - Common specifiers:
%d(int),%f(float),%c(char),%s(string) - Width controls minimum output width; precision controls decimal places
- Flags like
-,+,0,#modify alignment and display - Always match format specifiers to argument types to avoid undefined behavior
printf()returns the number of characters printed
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for printf() Function in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, input, output, printf, function, printf() function in c programming
Related C Programming Topics