Relearning MSX #36: Formatted output with printf()

Posted by in How-to, MSX, Retro, Technology | November 26, 2015


At this point we already know a decent amount about programming in C and we can create some useful (although simple) programs. We’ve seen how to display text and numbers on the screen, but we haven’t paid much attention to the presentation yet. We’re going to take care of this in the next few chapters.

There are several statements in MSX BASIC such as CLS and LOCATE to help determine where and how things will be displayed on the screen. The C language doesn’t have functions to do the same thing, but we use control characters and escape sequences to achieve the same thing.

Let’s start.

Aligning the digits in a number

In a previous post we saw that we can use printf() to output not only numbers in decimal notation, but also hexadecimal and octal as well. However, we can also use printf() to output aligned digits, print things other than just numbers, and output formatted data so it looks nice on the screen.

The printf() function supports three different alignments for numbers: right, left, and alignment to the right padded with zeroes to the left. The table below shows an example with decimal notation (%d), but note that this works exactly the same with all the other notations: %x, %o, %c and %u:


The red X represents a decimal number indicating the total number of spaces reserved to print the value.

Display a number aligned to the right

To display a decimal number (“%d”) aligned to the right we place between the “%” and the “d” the total width of the space where we’ll print the number:

%XXXd (replace XXX with a decimal number)


For example, if we have a space that’s 10 characters wide and we want to print the number “123” aligned to the right, we call printf() like this:

printf("%10d", 123);


This will print the number “123” preceded by 7 spaces so the total width will be 10 characters. Remember that this works in the same way with all the notations.

Display a number aligned to the left

This format works like the one before, but we enter the field width preceded by a minus sign:


For example:

printf("%-10d", 123);


In this case the number “123” appears on the screen followed by 7 spaces.

Right alignment padded with zeroes

It works like the alignment to the right, but instead of filling the empty positions with spaces, they’re filled with zeroes. The format string is like the first one, but with a zero before the field width:


For example:

printf("%010d", 123);

The following short program illustrates these examples:

Click to enlarge

Click to enlarge

As we said before, these alignments and field widths work with all numeric notations. We use them to format data in columns of constant width even if the values in the colums vary in length.

For example, we could write a small program to print a range of values in several different notations, using field width to align the values in columns:

Click to enlarge

Click to enlarge

Running this program will print the same value in four different representations. The values will be aligned in columns five characters wide even when the representations are of different length:

Click to enlarge

Click to enlarge

Printing using format strings and placeholders

So far we’ve used printf() to print single values on the screen, but this function is actually much more useful than that. We can use the format strings we saw earlier together with normal text, and even print more than one numeric value at a time.

Let’s see how.

Specifying the output format

The format strings we’ve seen so far (%d, %x and the rest) are actually placeholders. When printf() finds one of these, it replaces it with one of the arguments. We call the strings passed to printf() format strings because they define how the text will be printed on the screen.

It’s easier to understand with an example program:

Click to enlarge

Click to enlarge

As we can see in the example above, printf() substitutes only the %d placeholder with the value following the format string (in this case, 24 * 365), leaving all the other text as it is:


Of course, this applies to any of the placeholders we’ve seen (%x, %u, %o and %c), including the variations that contain alignment parameters.

There’s one more thing that you may be wondering about: if we use the % character as the start of a placeholder, how do we output a % character with printf()? The answer is to double the percent character, as in the example below:

Click to enlarge

Click to enlarge

Printing several values at a time

It’s also possible to output more than one value in a single printf() call. We just have to place as many placeholders as required in the format string, and as many parameters as needed, one per placeholder.

For example, the program below prints the three values 39, 402 and 39×402 in a single line:

Click to enlarge

Click to enlarge

As can be seen in the example, printf() just takes each placeholder and replaces them one by one with the argument values, in the same order as they appear:

format_string_example_multipleNow that we know this we could rewrite the PRINTF2.C program we saw earlier in this post to use a single call to printf() instead of five and even including the putchar() line after that:

Click to enlarge

Click to enlarge

This version of the program does exactly the same thing as the one we saw before, but it’s more efficient (faster) because it uses a single function call to do what we were doing with five printf()s and one putchar() in the previous version.


In this post we’ve seen a more advanced way to use printf(). We’ve learnt about placeholders and format strings, and how to use special placeholders to align numbers on the screen.

In the next post…

Next we’ll learn how to use control characters to perform basic screen management such as clearing the screen and resetting the cursor position. It will be a short post.

This series of articles is supported by your donations. If you’re willing and able to donate, please visit the link below to register a small pledge. Every little amount helps.

Javi Lavandeira’s Patreon page

Leave a Reply

Your email address will not be published. Required fields are marked *

Warning: Illegal string offset 'share_counts' in /www/javi_lavandeira/ on line 477