Skip to Content
Course content

18: Practice Exercise: Formatted Output Drills

Click on the "Edit" button in the top corner of the screen to edit your slide content.

How do I actually get my columns to line up when I'm printing a list?

I've seen a lot of beginners try to solve this by just hitting the spacebar a bunch of times in their strings. Please, don't do that. It looks fine on your monitor, but the moment a word is one character longer than you expected, the whole table shifts and looks like a mess.

The real way to handle this is using field width. When you put a number between the % and the type specifier, you're telling C the minimum number of characters that column should occupy. For example, %10s tells C to make the string at least 10 characters wide, padding the left side with spaces if it's shorter.

char* item = "Bolt";
int qty = 50;
// The %-10s left-aligns the string, the %5d right-aligns the integer
printf("%-10s | %5d\n", item, qty);
printf("%-10s | %5d\n", "Large Nut", 120);

Notice that minus sign in %-10s? That's a little trick to left-align the text. Without it, your words will be pushed to the right, which usually looks weird for text columns but great for numbers.

I'm getting six decimal places on my floats, but I only want two. How do I fix that?

By default, %f is quite chatty—it gives you six digits after the decimal point regardless of whether you need them. If you're printing something like a price or a percentage, that's just noise.

You handle this with a precision modifier: a dot followed by a number. %.2f tells C, "I only care about two digits after the decimal." It also handles rounding for you, which is a nice bonus.

double price = 19.995;
printf("Price: $%.2f\n", price); // This will print 20.00

I usually combine this with the field width we talked about earlier. If you want a price column that is 8 characters wide total with 2 decimal places, you'd use %8.2f.

Can I force a number to have a specific number of digits, like adding leading zeros to a clock or a date?

Yeah, this is common when you're formatting timestamps or ID numbers. If you just use %2d, C will pad the left side with a space. To get a zero instead, just put a 0 before the width.

Check this out:

int hours = 9;
int mins = 5;
int secs = 2;
printf("Time: %02d:%02d:%02d\n", hours, mins, secs);
// Output: Time: 09:05:02

It's a small detail, but it makes your output look professional rather than like a quick-and-dirty debug script.

What happens if the data is actually larger than the width I specified?

This is a common point of anxiety for people starting with printf. You might worry that %5d will chop off the first few digits of a 6-digit number.

Don't worry—C won't truncate your data. The width is a minimum, not a maximum. If your number is 1,000,000 but you specified %5d, C will just ignore the width and print the whole number. The downside is that your carefully aligned columns will be pushed out of whack. If you expect your numbers to grow, always set your width to the largest possible value you anticipate.




📋 Practical Task

Exercise: Hardware Component Inventory Report

Your task is to write a program that prints a formatted inventory report for a computer parts store. You need to display a header and three rows of data. Use the following requirements to ensure the output is perfectly aligned:

  • Headers: "Part Name", "Quantity", and "Unit Price".
  • Alignment:
    • Part Name should be left-aligned in a column 15 characters wide.
    • Quantity should be right-aligned in a column 10 characters wide.
    • Unit Price should be right-aligned in a column 10 characters wide, showing exactly 2 decimal places.
  • Data to use:
    • "RTX 3080", 5, 699.99
    • "DDR4 RAM", 24, 45.50
    • "NVMe SSD", 12, 120.00

Make sure there is a clear separator line (like a series of dashes) between the header and the data rows.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.