Skip to content

Commit 75b4ad9

Browse files
author
Colin Robertson
committed
Fix OS Bug 11012606:
1 parent e289b43 commit 75b4ad9

1 file changed

Lines changed: 46 additions & 55 deletions

File tree

docs/c-runtime-library/printf-p-positional-parameters.md

Lines changed: 46 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,105 +44,93 @@ translation.priority.ht:
4444
# printf_p Positional Parameters
4545
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:
4646

47-
[printf, _printf_l, wprintf, _wprintf_l](../c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l.md)
48-
[_printf_p, _printf_p_l, _wprintf_p, _wprintf_p_l](../c-runtime-library/reference/printf-p-printf-p-l-wprintf-p-wprintf-p-l.md)
47+
| Non-positional printf functions | Positional parameter equivalents |
48+
|---|---|
49+
|[printf, _printf_l, wprintf, _wprintf_l](../c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l.md)|[_printf_p, _printf_p_l, _wprintf_p, _wprintf_p_l](../c-runtime-library/reference/printf-p-printf-p-l-wprintf-p-wprintf-p-l.md)|
50+
|[sprintf, _sprintf_l, swprintf, _swprintf_l, \__swprintf_l](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)|[_sprintf_p, _sprintf_p_l, _swprintf_p, _swprintf_p_l](../c-runtime-library/reference/sprintf-p-sprintf-p-l-swprintf-p-swprintf-p-l.md)|
51+
|[_cprintf, _cprintf_l, _cwprintf, _cwprintf_l](../c-runtime-library/reference/cprintf-cprintf-l-cwprintf-cwprintf-l.md)|[_cprintf_p, _cprintf_p_l, _cwprintf_p, _cwprintf_p_l](../c-runtime-library/reference/cprintf-p-cprintf-p-l-cwprintf-p-cwprintf-p-l.md)|
52+
|[fprintf, _fprintf_l, fwprintf, _fwprintf_l](../c-runtime-library/reference/fprintf-fprintf-l-fwprintf-fwprintf-l.md)|[_fprintf_p, _fprintf_p_l, _fwprintf_p, _fwprintf_p_l](../c-runtime-library/reference/fprintf-p-fprintf-p-l-fwprintf-p-fwprintf-p-l.md)|
53+
|[vprintf, _vprintf_l, vwprintf, _vwprintf_l](../c-runtime-library/reference/vprintf-vprintf-l-vwprintf-vwprintf-l.md)|[_vprintf_p, _vprintf_p_l, _vwprintf_p, _vwprintf_p_l](../c-runtime-library/reference/vprintf-p-vprintf-p-l-vwprintf-p-vwprintf-p-l.md)|
54+
|[vfprintf, _vfprintf_l, vfwprintf, _vfwprintf_l](../c-runtime-library/reference/vfprintf-vfprintf-l-vfwprintf-vfwprintf-l.md)|[_vfprintf_p, _vfprintf_p_l, _vfwprintf_p, _vfwprintf_p_l](../c-runtime-library/reference/vfprintf-p-vfprintf-p-l-vfwprintf-p-vfwprintf-p-l.md)|
55+
|[vsprintf, _vsprintf_l, vswprintf, _vswprintf_l, \__vswprintf_l](../c-runtime-library/reference/vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l.md)|[_vsprintf_p, _vsprintf_p_l, _vswprintf_p, _vswprintf_p_l](../c-runtime-library/reference/vsprintf-p-vsprintf-p-l-vswprintf-p-vswprintf-p-l.md)|
4956

50-
[sprintf, _sprintf_l, swprintf, _swprintf_l, \__swprintf_l](../c-runtime-library/reference/sprintf-sprintf-l-swprintf-swprintf-l-swprintf-l.md)
51-
[_sprintf_p, _sprintf_p_l, _swprintf_p, _swprintf_p_l](../c-runtime-library/reference/sprintf-p-sprintf-p-l-swprintf-p-swprintf-p-l.md)
57+
## How to specify positional parameters
5258

53-
[_cprintf, _cprintf_l, _cwprintf, _cwprintf_l](../c-runtime-library/reference/cprintf-cprintf-l-cwprintf-cwprintf-l.md)
54-
[_cprintf_p, _cprintf_p_l, _cwprintf_p, _cwprintf_p_l](../c-runtime-library/reference/cprintf-p-cprintf-p-l-cwprintf-p-cwprintf-p-l.md)
59+
### Parameter indexing
60+
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](../c-runtime-library/format-specification-syntax-printf-and-wprintf-functions.md).
5561

56-
[fprintf, _fprintf_l, fwprintf, _fwprintf_l](../c-runtime-library/reference/fprintf-fprintf-l-fwprintf-fwprintf-l.md)
57-
[_fprintf_p, _fprintf_p_l, _fwprintf_p, _fwprintf_p_l](../c-runtime-library/reference/fprintf-p-fprintf-p-l-fwprintf-p-fwprintf-p-l.md)
62+
Here's an example of positional formatting:
5863

59-
[vprintf, _vprintf_l, vwprintf, _vwprintf_l](../c-runtime-library/reference/vprintf-vprintf-l-vwprintf-vwprintf-l.md)
60-
[_vprintf_p, _vprintf_p_l, _vwprintf_p, _vwprintf_p_l](../c-runtime-library/reference/vprintf-p-vprintf-p-l-vwprintf-p-vwprintf-p-l.md)
61-
62-
[vfprintf, _vfprintf_l, vfwprintf, _vfwprintf_l](../c-runtime-library/reference/vfprintf-vfprintf-l-vfwprintf-vfwprintf-l.md)
63-
[_vfprintf_p, _vfprintf_p_l, _vfwprintf_p, _vfwprintf_p_l](../c-runtime-library/reference/vfprintf-p-vfprintf-p-l-vfwprintf-p-vfwprintf-p-l.md)
64-
65-
[vsprintf, _vsprintf_l, vswprintf, _vswprintf_l, \__vswprintf_l](../c-runtime-library/reference/vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l.md)
66-
[_vsprintf_p, _vsprintf_p_l, _vswprintf_p, _vswprintf_p_l](../c-runtime-library/reference/vsprintf-p-vsprintf-p-l-vswprintf-p-vswprintf-p-l.md)
67-
68-
## Specifying Positional Parameters
69-
70-
##### Parameter Indexing
71-
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](../c-runtime-library/printf-type-field-characters.md).
72-
73-
For an example of this behavior:
74-
75-
```
64+
```C
7665
_printf_p("%1$s %2$s", "November", "10");
7766
```
7867
79-
will print
68+
This prints:
8069
8170
```
8271
November 10
8372
```
8473
85-
The order of the numbers used need not match the order of the arguments given. Thus the following is valid:
74+
The order of the numbers used doesn't need to match the order of the arguments. For example, this is a valid format string:
8675
87-
```
76+
```C
8877
_printf_p("%2$s %1$s", "November", "10");
8978
```
9079

91-
will print
80+
This prints:
9281

9382
```
9483
10 November
9584
```
9685

97-
Parameter may be used more than once while formatting, unlike in traditional format strings, so that
86+
Unlike traditional format strings, positional parameters may be used more than once in a format string. For example,
9887

99-
```
88+
```C
10089
_printf_p("%1$d times %1$d is %2$d", 10, 100);
10190
```
10291
103-
will print
92+
This prints:
10493
10594
```
10695
10 times 10 is 100
10796
```
10897
109-
However, all arguments must be used at least once somewhere in the format string.
110-
111-
The maximum number of positional parameters allowed in a format string is given by `_ARGMAX`.
98+
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`.
11299
113-
##### Width and Precision
114-
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 precision value must appear immediately following the \* symbol. For example,
100+
### Width and precision
101+
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,
115102
116-
```
103+
```C
117104
_printf_p("%1$*2$s","Hello", 10);
118105
```
119106

120-
or
107+
or
121108

122-
```
123-
_printf_p("%2$*1$s",10, "Hello");
109+
```C
110+
_printf_p("%2$*1$s", 10, "Hello");
124111
```
125112
126-
##### Mixing positional and non positional arguments
127-
Positional parameters may not be mixed with non-positional parameters in the same format string. However, `printf_p` and related functions still support non-positional parameters in format strings containing no positional parameters.
113+
### Mixing positional and non-positional arguments
114+
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.
128115
129116
## Example
130117
131-
```
118+
```C
132119
// positional_args.c
120+
// Build by using: cl /W4 positional_args.c
133121
// Positional arguments allow the specification of the order
134122
// in which arguments are consumed in a formatting string.
135123
136124
#include <stdio.h>
137125
138-
int main(int argc, char *argv[])
126+
int main()
139127
{
140128
int i = 1,
141129
j = 2,
142130
k = 3;
143131
double x = 0.1,
144-
y = 0.2,
145-
z = 0.3;
132+
y = 2.22,
133+
z = 333.3333;
146134
char *s1 = "abc",
147135
*s2 = "def",
148136
*s3 = "ghi";
@@ -151,25 +139,28 @@ int main(int argc, char *argv[])
151139
// normal input order is used.
152140
_printf_p("%d %d %d\n", i, j, k);
153141
154-
// Positional args are numbers indicating the
155-
// argument enclosed in curly braces.
142+
// Positional arguments are numbers followed by a $ character.
156143
_printf_p("%3$d %1$d %2$d\n", i, j, k);
157144
158145
// The same positional argument may be reused.
159146
_printf_p("%1$d %2$d %1$d\n", i, j);
160147
148+
// The positional arguments may appear in any order.
161149
_printf_p("%1$s %2$s %3$s\n", s1, s2, s3);
162-
163150
_printf_p("%3$s %1$s %2$s\n", s1, s2, s3);
151+
152+
// Precision and width specifiers must be int types.
153+
_printf_p("%3$*5$f %2$.*4$f %1$*4$.*5$f\n", x, y, z, j, k);
164154
}
165155
```
166156

167157
```Output
168-
1 2 3
169-
3 1 2
170-
1 2 1
171-
abc def ghi
172-
ghi abc def
158+
1 2 3
159+
3 1 2
160+
1 2 1
161+
abc def ghi
162+
ghi abc def
163+
333.333300 2.22 0.100
173164
```
174165

175166
## See Also

0 commit comments

Comments
 (0)