printf_p Positional Parameters
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at printf_p Positional Parameters.
Positional parameters provide the ability to specify by number which of the arguments is to be substituted into a field in a format string. The following positional parameter printf
functions are available:
How to specify positional parameters
Parameter indexing
By default, if no positional formatting is present, the positional functions behave identically to the non-positional ones. You specify the positional parameter to format by using %n$
at the beginning of the format specifier, where n
is the position of the parameter to format in the parameter list. The parameter position starts at 1 for the first argument after the format string. The remainder of the format specifier follows the same rules as the printf
format specifier. For more information about format specfiers, see Format Specification Syntax: printf and wprintf Functions.
Here's an example of positional formatting:
_printf_p("%1$s %2$s", "November", "10");
This prints:
November 10
The order of the numbers used doesn't need to match the order of the arguments. For example, this is a valid format string:
_printf_p("%2$s %1$s", "November", "10");
This prints:
10 November
Unlike traditional format strings, positional parameters may be used more than once in a format string. For example,
_printf_p("%1$d times %1$d is %2$d", 10, 100);
This prints:
10 times 10 is 100
All arguments must be used at least once somewhere in the format string. The maximum number of positional parameters allowed in a format string is given by _ARGMAX
.
Width and precision
You can use *n$
to specify a positional parameter as a width or precision specifier, where n
is the position of the width or precision parameter in the parameter list. The position of the width or precision value must appear immediately following the * symbol. For example,
_printf_p("%1$*2$s","Hello", 10);
or
_printf_p("%2$*1$s", 10, "Hello");
Mixing positional and non-positional arguments
Positional parameters may not be mixed with non-positional parameters in the same format string. If any positional formatting is used, all format specifiers must use positional formatting. However, printf_p
and related functions still support non-positional parameters in format strings containing no positional parameters.
Example
// positional_args.c
// Build by using: cl /W4 positional_args.c
// Positional arguments allow the specification of the order
// in which arguments are consumed in a formatting string.
#include <stdio.h>
int main()
{
int i = 1,
j = 2,
k = 3;
double x = 0.1,
y = 2.22,
z = 333.3333;
char *s1 = "abc",
*s2 = "def",
*s3 = "ghi";
// If positional arguments are unspecified,
// normal input order is used.
_printf_p("%d %d %d\n", i, j, k);
// Positional arguments are numbers followed by a $ character.
_printf_p("%3$d %1$d %2$d\n", i, j, k);
// The same positional argument may be reused.
_printf_p("%1$d %2$d %1$d\n", i, j);
// The positional arguments may appear in any order.
_printf_p("%1$s %2$s %3$s\n", s1, s2, s3);
_printf_p("%3$s %1$s %2$s\n", s1, s2, s3);
// Precision and width specifiers must be int types.
_printf_p("%3$*5$f %2$.*4$f %1$*4$.*5$f\n", x, y, z, j, k);
}
1 2 3
3 1 2
1 2 1
abc def ghi
ghi abc def
333.333300 2.22 0.100
See Also
printf Type Field Characters
printf Width Specification
Precision Specification