Conversion Character Mania: The Rest

I don’t think I’ve ever seen the full lot of printf() conversion characters explained in any detail. This lesson is the last in a series of my attempt to do just that.

First, a recap. Here are all the major printf() conversion characters:

d, i Integer values output as a decimal number
u Unsigned integer values output as a decimal number
f Floating point (real) values, float or double
e, E Floating point values output using scientific notation
g, G Floating point values output using scientific notation if the value is huge or as a real value if it’s small
c Character values output as a single character (text)
s Strings output as text
x, X Integer values output as hexadecimal, upper or lower case letters
o Integer values output as octal
p Memory address (pointer variable) output usually in hexadecimal
% Used to output a %

Previous lessons have covered most of these conversion characters. What remains are %x/%X, %o, %p, and %%.

First the easy one: %% outputs a single percent sign, %. Because the % is used as the conversion character prefix, %% is required whenever you need to output a % by using the printf() function. Remember that or a compiler warning will remind you.

The %x and %X conversion characters output an integer value in hexadecimal. When %x is used, hex numbers A through F are displayed in lower case; %X outputs them in upper case.

The %o conversion character outputs integer values using octal or base eight. That was very popular when the C language was developed back in the 1970s, but not so popular today. In fact, I’m pretty offended by octal these days.

Like the other integer output functions, you can add a width option and - for justification with %x/%X and %c. A leading zero on the width option pads the left side of output with zeros. Here’s your sample code:

#include <&stdio.h>

int main()
{
    int value;

    printf("Type an integer: ");
    scanf("%d",&value);

    printf("The value: %8d\n",value);
    printf("With %%X:   %8X\n",value);
    printf("With %%x:   %8x\n",value);
    printf("With %%o:   %8o\n",value);

    return(0);
}

Sample run:

Type an integer: 54321
The value:    54321
With %X:       D431
With %x:       d431
With %o:     152061

The %p conversion character displays the address (memory location) of a pointer variable, or of any variable prefixed by the & operator. The output is often in hexadecimal, though the format for that number depends on the compiler or computing platform.

I typically use %p for debugging purposes, to discover a memory location or to demonstrate how pointers work. Beyond that it’s not too popular.

#include <stdio.h>

int main()
{
    int array[3] = { 397, 457, 521 };
    int x;

    for(x=0;x<3;x++)
        printf("%d: %p %d\n",
            x+1,
            &array[x],
            array[x]);

    return(0);
}

Sample run:

1: 0x7fff4fc37abc 397
2: 0x7fff4fc37ac0 457
3: 0x7fff4fc37ac4 521

The memory locations generated by %p will be different on your computer, and may even carry a different format.

Your compiler may also support non-standard conversion characters. You need to check the printf() man page for their availability. There's no guarantee, however, that those specific options work or will even compile on other computing platforms.

Leave a Reply