Skip to content

I/O Format Specifiers for Writing Integers

An integer argument has a format specifier of the following form:

%[flags] [488.2 type] [width] [.precision] [delimiter [array size] ] [$S] [b | h | l | I] [d | o | x]

ModifierInterpretation
Default FunctionalityThe argument is formatted as an integer in NR1 format (an integer without a decimal point).
flags +, -, 0Controls justification and padding of the output, as follows:
- - left aligns the result with the given field width.
+ - prefixes the output value with a sign (+ or –) if the output value is of a signed type. By default, a sign only appears for negative values.
0 pads with zeros until the specified width is reached. If 0 and - appear together, the 0 is ignored.
488.2 typeThe number is formatted in one of six standard IEEE 488.2 numeric formats. The supported formats are:
@1 - IEEE 488.2 NR1 format (integer without any decimal point). Example: 123
@2 - IEEE 488.2 NR2 format (number with at least one digit after the decimal point). Example: 123.45
@3 - IEEE 488.2 NR3 format (floating-point number in exponential form). Example: 1.2345E-67
@H - IEEE 488.2 hex format. Example: #HAF35B
@Q - IEEE 488.2 octal format. Example: #Q71234
@B - IEEE 488.2 binary format. Example: #B011101001
widthMinimum field width of the output number. An asterisk (*) may be present in lieu of an integer width modifier, in which case an extra int argument supplies the value.
precisionMinimum number of digits to send. An asterisk (*) may be present in lieu of an integer precision modifier, in which case an extra int argument supplies the value.
delimiter [array size]The argument is interpreted as an array of integers (or long or short integers, as specified by the length). If the $S modifier is present, the argument is interpreted as a SAFEARRAY of integers. array size specifies the number of array elements to send. An asterisk (*) may be present in lieu of an integer array size modifier, in which case an extra int argument supplies the value.
$SThe argument is interpreted as a SAFEARRAY of integers.
length modifierIf no length modifier is specified, then the argument is assumed to be a long integer (32-bits).
b - argument is a BYTE (8-bit) integer.
h - argument is a short (16-bit) integer.
l - argument is a long (32-bit) integer.
I - argument is a __int64 (64-bit) integer.
typed - number is formatted as a decimal integer.
o - number is formatted as an octal integer.
x - number is formatted as a hex integer.
HRESULT hr = S_OK;
int nCount = 1234;
// Left justify
hr = io.Printf(_T("%-8d"), nCount); // Sends "1234 "
// Right justify
hr = io.Printf(_T("%8d"), nCount); // Sends " 1234"
// Right justify, pad with zeroes
hr = io.Printf(_T("%08d"), nCount); // Sends "00001234"
// Supply field width using a variable
int nWidth = 5;
hr = io.Printf(_T("%0*d"), nWidth, lCount); // Sends "00123";
HRESULT hr = S_OK;
int nCount = 1234;
hr = io.Printf(_T("%d"), nCount); // Sends "1234"
long lCount = 1234;
hr = io.Printf(_T("%ld"), lCount); // Sends "1234"
short sCount = 1234;
hr = io.Printf(_T("%hd"), sCount); // Sends "1234"
HRESULT hr = S_OK;
int rgData[] = { 1, 2, 3, 4, 5 };
// Comma-separated list of 3 elements
hr = io.Printf(_T("%,3d"), rgData); // Sends "1,2,3"
// Semi-colon separated, count supplied as a variable
int nCount = 4;
hr = io.Printf(_T("%(;)*d"), nCount, rgData); // Sends "1;2;3;4"
// Field width specified applies to each element
hr = io.Printf(_T("%4,3d"), rgData); // Sends " 1, 2, 3"
CComSafeArray<int> saData;
saData.Add(1);
saData.Add(3);
saData.Add(3);
// Comma-separated list. Argument is a SAFEARRAY.
// No field width supplied, so count is determined from the SAFEARRAY itself.
hr = io.Printf(_T("%,$Sd"), saData.m_psa); // Sends "1,2,3"
HRESULT hr = S_OK;
// Format value as an IEEE-defined hex number
int nMask = 1234;
hr = io.Printf(_T("%@Hd"), nMask); // Sends "#H7B"
// Format value as an IEEE-defined binary number
hr = io.Printf(_T("%@Hd"), nMask); // Sends "#B1111011"
int rgData[] = { 1, 2, 3, 4, 5 };
// Field width of 5 includes the "#B", pad with zeros
hr = io.Printf(_T("%@B05,5d"), rgData); // Sends "#B001,#B010,#B011,#B100,#B101"