Width, Precision, Flags
Control formatting.
Width, Precision, Flags is a free C Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
Controlling Output Width
A number after % sets the minimum field width. The value is right-aligned and padded with spaces.
#include <stdio.h>
int main(void) {
printf("[%5d]\n", 42);
return 0;
}Left Alignment
A - flag left-aligns the value within the field.
#include <stdio.h>
int main(void) {
printf("[%-5d]\n", 42);
return 0;
}Zero Padding
A 0 flag pads numbers with leading zeros instead of spaces.
#include <stdio.h>
int main(void) {
printf("[%05d]\n", 42);
return 0;
}Precision for Floats
.N sets how many digits appear after the decimal point.
#include <stdio.h>
int main(void) {
printf("%.2f\n", 3.14159);
return 0;
}Width and Precision Together
Combine them as %width.precisionf for aligned decimal columns.
#include <stdio.h>
int main(void) {
printf("[%8.2f]\n", 3.14159);
return 0;
}Precision for Strings
With %s, precision limits how many characters are printed.
#include <stdio.h>
int main(void) {
printf("[%.3s]\n", "Hello");
return 0;
}The Plus Flag
The + flag forces a sign to appear even for positive numbers.
#include <stdio.h>
int main(void) {
printf("%+d %+d\n", 7, -7);
return 0;
}Space Flag
A space flag prints a leading space for positive numbers, useful for alignment with negatives.
#include <stdio.h>
int main(void) {
printf("% d\n% d\n", 7, -7);
return 0;
}Building a Table
Field widths line up columns neatly.
#include <stdio.h>
int main(void) {
printf("%-10s%5d\n", "Apples", 12);
printf("%-10s%5d\n", "Pears", 7);
return 0;
}Currency Formatting
Two decimals plus a width gives a clean money column.
#include <stdio.h>
int main(void) {
printf("$%8.2f\n", 1234.5);
printf("$%8.2f\n", 9.99);
return 0;
}Dynamic Width
An asterisk lets you pass the width as an argument at runtime.
#include <stdio.h>
int main(void) {
int w = 6;
printf("[%*d]\n", w, 42);
return 0;
}Quick Check
Test your understanding of width and precision.
Recap: Width, Precision, Flags
You learned to format output precisely.
- A width number sets minimum field size
-left-aligns,0zero-pads.Nsets decimal precision for floats and length for strings+and space control the sign*supplies width at runtime
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Width, Precision, Flags” lesson free?
Yes — the full text of “Width, Precision, Flags” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “Width, Precision, Flags”?
Control formatting. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Width, Precision, Flags” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- printf Format Specifiers
- scanf and Input
- Width, Precision, Flags
- Common I/O Pitfalls