Click or drag to resize

I/O Format Specifiers for Writing Characters

Character Data -- %c

A character argument has a format specifier of the following form:

%[flags] [width] [.precision] c

Modifier

Interpretation

Default Functionality

The argument is interpreted as a single-byte character. The character is sent to the device without change.

flags

-, 0

Controls justification and padding of the output, as follows:

  • - - left aligns the result with the given field width.

  • 0 - pads with zeros until the width is reached. If 0 and - appear together, the 0 is ignored.

width

Minimum field width of the output string.

An asterisk (*) may be present in lieu of an integer width modifier, in which case an extra int argument supplies the value.

precision

Maximum number of characters to send.

An asterisk (*) may be present in lieu of an integer precision modifier, in which case an extra int argument supplies the value.

Example -- Single-character output

C++
HRESULT hr = S_OK;

char ch = 'A';

hr = io.Printf(_T("%c"), ch);                 // Sends "A"

// Field width specifies minimum width, right justify and pad to the left
hr = io.Printf(_T("%5c"), ch);                // Sends "    A"

// Left justify and pad to the right, field width specified with a variable
int nWidth = 5;
hr = io.Printf(_T("%-*c"), nWidth, ch);       // Sends "A    "
See Also

Other Resources

Download a complete CHM version of this documentation here.