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
Function declarations in which the last member of is the ellipsis (...) can take a variable number of arguments. In these cases, C++ provides type checking only for the explicitly declared arguments. You can use variable argument lists when you need to make a function so general that even the number and types of arguments can vary. The `printf` family of functions is an example of functions that use variable argument lists.
9
+
Function declarations that have ellipsis (...) as the last argument take a variable number of arguments. C++ provides type checking only for the explicitly declared arguments. You can use variable argument lists when the number and types of arguments to the function can vary. The `printf` family of functions is an example of functions that have variable argument lists.
10
10
11
11
## Functions with variable arguments
12
12
13
-
To access arguments after those declared, use the macros contained in the standard include file `<stdarg.h>` as described below.
13
+
To access arguments after those declared, use the macros contained in the standard include file `<stdarg.h>` as explained in this article.
14
14
15
15
**Microsoft Specific**
16
16
17
-
Microsoft C++ allows the ellipsis to be specified as an argument if the ellipsis is the last argument and the ellipsis is preceded by a comma. Therefore, the declaration `int Func( int i, ... );` is legal, but `int Func( int i ... );` is not.
17
+
Microsoft C++ allows the ellipsis to be specified as an argument if the ellipsis is the last argument and a comma comes before the ellipsis. Therefore, the declaration `int Func( int i, ... );` is legal, but `int Func( int i ... );` is not.
18
18
19
19
**END Microsoft Specific**
20
20
21
21
Declaration of a function that takes a variable number of arguments requires at least one placeholder argument, even if it is not used. If this placeholder argument is not supplied, there is no way to access the remaining arguments.
22
22
23
-
When arguments of type **`char`** are passed as variable arguments, they are converted to type **`int`**. Similarly, when arguments of type **`float`** are passed as variable arguments, they are converted to type **`double`**. Arguments of other types are subject to the usual integral and floating-point promotions. See [Standard Conversions](standard-conversions.md) for more information.
23
+
When arguments of type **`char`** are passed as variable arguments, they are converted to type **`int`**. Similarly, when arguments of type **`float`** are passed as variable arguments, they are converted to type **`double`**. Arguments of other types are subject to the usual integral and floating-point promotions. For more information, see [Standard Conversions](standard-conversions.md).
24
24
25
-
Functions that require variable lists are declared by using the ellipsis (...) in the argument list. Use the types and macros that are described in the `<stdarg.h>` include file to access arguments that are passed by a variable list. For more information about these macros, see [va_arg, va_copy, va_end, va_start](../c-runtime-library/reference/va-arg-va-copy-va-end-va-start.md). in the documentation for the C Run-Time Library.
25
+
Functions that require variable lists are declared by using the ellipsis (...) in the argument list. Use the types and macros that are described in the `<stdarg.h>` include file to access arguments that are passed by a variable list. For more information about these macros, see [va_arg, va_copy, va_end, va_start](../c-runtime-library/reference/va-arg-va-copy-va-end-va-start.md).
26
26
27
-
The following example shows how the macros work together with the type (declared in `<stdarg.h>`):
27
+
The following example shows how to use the macros to process a variable argument list:
28
28
29
29
```cpp
30
30
// variable_argument_lists.cpp
31
+
31
32
#include<stdio.h>
32
33
#include<stdarg.h>
33
34
34
35
// Declaration, but not definition, of ShowVar.
35
36
voidShowVar( char *szTypes, ... );
37
+
36
38
int main() {
37
39
ShowVar( "fcsi", 32.4f, 'a', "Test string", 4 );
38
40
}
39
41
40
-
// ShowVar takes a format string of the form
41
-
// "ifcs", where each character specifies the
42
-
// type of the argument in that position.
42
+
// ShowVar takes a format string of the form
43
+
// "ifcs", where each character specifies the
44
+
// type of the argument in that position.
43
45
//
44
-
// i = int
45
-
// f = float
46
-
// c = char
47
-
// s = string (char *)
46
+
// i = int
47
+
// f = float
48
+
// c = char
49
+
// s = string (char *)
48
50
//
49
-
// Following the format specification is a variable
50
-
// list of arguments. Each argument corresponds to
51
-
// a format character in the format string to which
51
+
// Following the format specification is a variable
52
+
// list of arguments. Each argument corresponds to
53
+
// a format character in the format string to which
52
54
// the szTypes parameter points
53
55
void ShowVar( char *szTypes, ... ) {
54
56
va_list vl;
55
57
int i;
56
58
57
-
// szTypes is the last argument specified; you must access
58
-
// all others using the variable-argument macros.
59
+
// szTypes is the last argument specified; you must access
60
+
// all others using the variable-argument macros.
59
61
va_start( vl, szTypes );
60
62
61
63
// Step through the list.
62
64
for( i = 0; szTypes[i] != '\0'; ++i ) {
65
+
63
66
union Printable_t {
64
67
int i;
65
68
float f;
66
69
char c;
67
70
char *s;
68
71
} Printable;
69
72
70
-
switch( szTypes[i] ) { // Type to expect.
73
+
switch( szTypes[i] ) { // Type to expect
71
74
case 'i':
72
75
Printable.i = va_arg( vl, int );
73
76
printf_s( "%i\n", Printable.i );
74
-
break;
77
+
break;
75
78
76
79
case 'f':
77
80
Printable.f = va_arg( vl, double );
78
81
printf_s( "%f\n", Printable.f );
79
-
break;
82
+
break;
80
83
81
84
case 'c':
82
85
Printable.c = va_arg( vl, char );
83
86
printf_s( "%c\n", Printable.c );
84
-
break;
87
+
break;
85
88
86
89
case 's':
87
90
Printable.s = va_arg( vl, char * );
88
91
printf_s( "%s\n", Printable.s );
89
-
break;
92
+
break;
90
93
91
94
default:
92
-
break;
95
+
break;
93
96
}
94
97
}
95
98
va_end( vl );
96
99
}
97
-
//Output:
98
-
// 32.400002
99
-
// a
100
-
// Test string
100
+
101
+
```output
102
+
32.400002
103
+
a
104
+
Test string
101
105
```
102
106
103
107
The previous example illustrates these important concepts:
0 commit comments