Conversion Character Mania: Integer Output

The printf() function’s power lies in its formatting abilities, specifically the display of values. That power is vast, but the documentation showing how it works really sucks.

Rather than try to create this massive grid that shows all the printf() conversion character options and settings, I thought I’d concentrate on presenting each conversion character based on the type of value it formats. This week’s lesson is about the integer output conversion characters.

Because integer values can be either signed or unsigned, C uses two different conversion characters to format output: %d for signed int values and %u for unsigned.

The %d and %u conversion characters handle the variable types shown in Table 1.

  %d %u
int X  
short X  
signed int X  
unsigned   X
unsigned short   X
unsigned int   X
char X  
unsigned char   X

The same conversion characters are used for long int values as well. In that case you prefix the d or u with an l (lower case L): %ld and %lu

The double long or long long integer values use two Ls: %lld and %llu

You can sandwich a width value between the percent sign and the conversion character letters. This value specifies how many characters wide to format the output. The purpose of the width value is to help align output to a fixed field.

In the following code, the value 8 is used to set the output to 8 characters.

#include <stdio.h>

int main()
{
    int i = 498;

    printf("Value:%8d\n",i);
    printf("Value:%8d\n",i*10);
    printf("Value:%8d\n",i*100);

    return(0);
}

The integer is right-aligned in the output, which looks like this:

Value:     498
Value:    4980
Value:   49800

The width value can apparently be any size; I’ve tested it up to 4,300,000,000 characters wide and the compiler didn’t puke.

If, for some bizarre reason, you wanted to left-justify the values, change the placeholder to %-8d in each printf() statement. Here’s the output:

Value:498
Value:4980
Value:49800

You don’t see it, but spaces are output to the right of the numbers above.

Another prefix you can toss into integer output is 0. What it does is to prefix output with zeros to pad the specified width. The 0 has to appear before the width value.

For example, if you change all the %8d conversion characters in the above code to %08d, the output looks like this:

Value:00000498
Value:00004980
Value:00049800

The output is still 8 characters wide, but zeros pad each value on the left.

In case you’re curious, you can’t use both - and 0 together. If you want to pad zeros on the right side of a number, you’ll need to concoct your own function.

To prefix a sign (+ or -) to the output, place a + character right after the % symbol. That character is inserted along with other options except the 0 prefix. All of these formats are valid:

printf("Value:%8d\n",678);
printf("Value:%08d\n",678);
printf("Value:%+8d\n",678);
printf("Value:%-8d\n",678);
printf("Value:%+-8d\n",678);

Here’s the output for those five statements:

Value:     678
Value:00000678
Value:    +678
Value:678     
Value:+678  

In case you’re curious, you cannot prefix a currency symbol to the output by using a conversion character. Something like %$d or %$8d isn’t valid, although it might be in other programming languages.

And now, the stragglers.

Some integer conversion characters linger from the early days of C, although they’re pretty much unused these days. For example, the %i conversion character displays integer values, but I recommend using %d instead.

The h prefix can be used like l, but to display short int values. For example, %hd ensures that a short int value is output. I believe the h stands for “half” as in half an int value. (Internally, that means the variable would be represented by two bytes instead of four.)

I’ll cover conversion character options for displaying floating point values in next week’s Lesson.

Leave a Reply