Skip to content

Commit faf507d

Browse files
committed
acrolinx
1 parent 5d5b730 commit faf507d

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

docs/cpp/functions-with-variable-argument-lists-cpp.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,98 +6,102 @@ helpviewer_keywords: ["arguments [C++], variable number of", "variable argument
66
---
77
# Functions with Variable Argument Lists (C++)
88

9-
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.
1010

1111
## Functions with variable arguments
1212

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.
1414

1515
**Microsoft Specific**
1616

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.
1818

1919
**END Microsoft Specific**
2020

2121
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.
2222

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).
2424

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).
2626

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:
2828

2929
```cpp
3030
// variable_argument_lists.cpp
31+
3132
#include <stdio.h>
3233
#include <stdarg.h>
3334

3435
// Declaration, but not definition, of ShowVar.
3536
void ShowVar( char *szTypes, ... );
37+
3638
int main() {
3739
ShowVar( "fcsi", 32.4f, 'a', "Test string", 4 );
3840
}
3941

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.
4345
//
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 *)
4850
//
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
5254
// the szTypes parameter points
5355
void ShowVar( char *szTypes, ... ) {
5456
va_list vl;
5557
int i;
5658

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.
5961
va_start( vl, szTypes );
6062

6163
// Step through the list.
6264
for( i = 0; szTypes[i] != '\0'; ++i ) {
65+
6366
union Printable_t {
6467
int i;
6568
float f;
6669
char c;
6770
char *s;
6871
} Printable;
6972

70-
switch( szTypes[i] ) { // Type to expect.
73+
switch( szTypes[i] ) { // Type to expect
7174
case 'i':
7275
Printable.i = va_arg( vl, int );
7376
printf_s( "%i\n", Printable.i );
74-
break;
77+
break;
7578

7679
case 'f':
7780
Printable.f = va_arg( vl, double );
7881
printf_s( "%f\n", Printable.f );
79-
break;
82+
break;
8083

8184
case 'c':
8285
Printable.c = va_arg( vl, char );
8386
printf_s( "%c\n", Printable.c );
84-
break;
87+
break;
8588

8689
case 's':
8790
Printable.s = va_arg( vl, char * );
8891
printf_s( "%s\n", Printable.s );
89-
break;
92+
break;
9093

9194
default:
92-
break;
95+
break;
9396
}
9497
}
9598
va_end( vl );
9699
}
97-
//Output:
98-
// 32.400002
99-
// a
100-
// Test string
100+
101+
```output
102+
32.400002
103+
a
104+
Test string
101105
```
102106

103107
The previous example illustrates these important concepts:

0 commit comments

Comments
 (0)