Udostępnij za pośrednictwem


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:

Specifying Positional Parameters

Parameter Indexing

By default the positional functions behave identically to the non position ones, if no positional formatting is present. Positional parameters are specified using the format "%m$x", where m denotes a numeric ordinal number indicating the position of the parameter in the list of parameters, preceding the format string and x denotes the type field character type specified in the printf function. The parameters in the list are indexed starting at the value 1 for the first element in the list and so forth. For additional information concerning type field characters, see printf Type Field Characters.

For an example of this behavior:

_printf_p("%1$s %2$s", "November", "10");

will print

November 10

The order of the numbers used need not match the order of the arguments given. Thus the following is valid:

_printf_p("%2$s %1$s", "November", "10");

will print

10 November

Parameter may be used more than once while formatting, unlike in traditional format strings, so that

_printf_p("%{1$d times %1$d is %2$d", 10, 100);

will print

10 times 10 is 100

However, 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 Prevision

When the * symbol is used to specify that the width or precision is to be determined from an argument, then the position of the width or prevision 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. However, printf_s and related functions still support non-positional parameters in format strings containing no positional parameters.

Example

// 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 argc, char *argv[])
{
    int     i = 1,
            j = 2,
            k = 3;
    double  x = 0.1,
            y = 0.2,
            z = 0.3;
    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 args are numbers indicating the
    // argument enclosed in curly braces.
    _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);

    _printf_p("%1$s %2$s %3$s\n", s1, s2, s3);

    _printf_p("%3$s %1$s %2$s\n", s1, s2, s3);
}

1 2 3 3 1 2 1 2 1 abc def ghi ghi abc def

See Also

Reference

printf Type Field Characters

printf Width Specification

Precision Specification