You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/c-runtime-library/printf-p-positional-parameters.md
+46-55Lines changed: 46 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,105 +44,93 @@ translation.priority.ht:
44
44
# printf_p Positional Parameters
45
45
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:
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).
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
76
65
_printf_p("%1$s %2$s", "November", "10");
77
66
```
78
67
79
-
will print
68
+
This prints:
80
69
81
70
```
82
71
November 10
83
72
```
84
73
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:
86
75
87
-
```
76
+
```C
88
77
_printf_p("%2$s %1$s", "November", "10");
89
78
```
90
79
91
-
will print
80
+
This prints:
92
81
93
82
```
94
83
10 November
95
84
```
96
85
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,
98
87
99
-
```
88
+
```C
100
89
_printf_p("%1$d times %1$d is %2$d", 10, 100);
101
90
```
102
91
103
-
will print
92
+
This prints:
104
93
105
94
```
106
95
10 times 10 is 100
107
96
```
108
97
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`.
112
99
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,
115
102
116
-
```
103
+
```C
117
104
_printf_p("%1$*2$s","Hello", 10);
118
105
```
119
106
120
-
or
107
+
or
121
108
122
-
```
123
-
_printf_p("%2$*1$s",10, "Hello");
109
+
```C
110
+
_printf_p("%2$*1$s",10, "Hello");
124
111
```
125
112
126
-
#####Mixing positional and nonpositional 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.
128
115
129
116
## Example
130
117
131
-
```
118
+
```C
132
119
// positional_args.c
120
+
// Build by using: cl /W4 positional_args.c
133
121
// Positional arguments allow the specification of the order
134
122
// in which arguments are consumed in a formatting string.
135
123
136
124
#include <stdio.h>
137
125
138
-
int main(int argc, char *argv[])
126
+
int main()
139
127
{
140
128
int i = 1,
141
129
j = 2,
142
130
k = 3;
143
131
double x = 0.1,
144
-
y = 0.2,
145
-
z = 0.3;
132
+
y = 2.22,
133
+
z = 333.3333;
146
134
char *s1 = "abc",
147
135
*s2 = "def",
148
136
*s3 = "ghi";
@@ -151,25 +139,28 @@ int main(int argc, char *argv[])
151
139
// normal input order is used.
152
140
_printf_p("%d %d %d\n", i, j, k);
153
141
154
-
// Positional args are numbers indicating the
155
-
// argument enclosed in curly braces.
142
+
// Positional arguments are numbers followed by a $ character.
156
143
_printf_p("%3$d %1$d %2$d\n", i, j, k);
157
144
158
145
// The same positional argument may be reused.
159
146
_printf_p("%1$d %2$d %1$d\n", i, j);
160
147
148
+
// The positional arguments may appear in any order.
161
149
_printf_p("%1$s %2$s %3$s\n", s1, s2, s3);
162
-
163
150
_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);
0 commit comments