Skip to content

Commit 9a6d1c1

Browse files
authored
Merge pull request #5899 from TylerMSFT/UUF
Uuf
2 parents b6a4fe8 + 6d5870b commit 9a6d1c1

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed
Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,113 @@
11
---
22
description: "Learn more about: Functions with Variable Argument Lists (C++)"
33
title: "Functions with Variable Argument Lists (C++)"
4-
ms.date: "11/04/2016"
4+
ms.date: 05/01/2025
55
helpviewer_keywords: ["arguments [C++], variable number of", "variable argument lists", "declarators, functions", "argument lists [C++], variable number of", "declaring functions [C++], variables", "function calls, variable number of arguments"]
6-
ms.assetid: 27c2f83a-21dd-44c6-913c-2834cb944703
76
---
87
# Functions with Variable Argument Lists (C++)
98

10-
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 family of functions is an example of functions that use variable argument lists.`printf`*argument-declaration-list*
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.
1110

1211
## Functions with variable arguments
1312

14-
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.
1514

1615
**Microsoft Specific**
1716

18-
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 ... );` isn't.
1918

2019
**END Microsoft Specific**
2120

22-
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.
21+
Declaration of a function that takes a variable number of arguments requires at least one placeholder argument, even if it isn't used. If this placeholder argument isn't supplied, there's no way to access the remaining arguments.
2322

24-
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're converted to type **`int`**. Similarly, when arguments of type **`float`** are passed as variable arguments, they're 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).
2524

26-
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).
2726

28-
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:
2928

3029
```cpp
3130
// variable_argument_lists.cpp
31+
3232
#include <stdio.h>
3333
#include <stdarg.h>
3434

3535
// Declaration, but not definition, of ShowVar.
3636
void ShowVar( char *szTypes, ... );
37+
3738
int main() {
3839
ShowVar( "fcsi", 32.4f, 'a', "Test string", 4 );
3940
}
4041

41-
// ShowVar takes a format string of the form
42-
// "ifcs", where each character specifies the
43-
// 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.
4445
//
45-
// i = int
46-
// f = float
47-
// c = char
48-
// s = string (char *)
46+
// i = int
47+
// f = float
48+
// c = char
49+
// s = string (char *)
4950
//
50-
// Following the format specification is a variable
51-
// list of arguments. Each argument corresponds to
52-
// 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
5354
// the szTypes parameter points
5455
void ShowVar( char *szTypes, ... ) {
5556
va_list vl;
5657
int i;
5758

58-
// szTypes is the last argument specified; you must access
59-
// 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.
6061
va_start( vl, szTypes );
6162

6263
// Step through the list.
6364
for( i = 0; szTypes[i] != '\0'; ++i ) {
65+
6466
union Printable_t {
6567
int i;
6668
float f;
6769
char c;
6870
char *s;
6971
} Printable;
7072

71-
switch( szTypes[i] ) { // Type to expect.
73+
switch( szTypes[i] ) { // Type to expect
7274
case 'i':
7375
Printable.i = va_arg( vl, int );
7476
printf_s( "%i\n", Printable.i );
75-
break;
77+
break;
7678

7779
case 'f':
7880
Printable.f = va_arg( vl, double );
7981
printf_s( "%f\n", Printable.f );
80-
break;
82+
break;
8183

8284
case 'c':
8385
Printable.c = va_arg( vl, char );
8486
printf_s( "%c\n", Printable.c );
85-
break;
87+
break;
8688

8789
case 's':
8890
Printable.s = va_arg( vl, char * );
8991
printf_s( "%s\n", Printable.s );
90-
break;
92+
break;
9193

9294
default:
93-
break;
95+
break;
9496
}
9597
}
9698
va_end( vl );
9799
}
98-
//Output:
99-
// 32.400002
100-
// a
101-
// Test string
100+
```
101+
102+
```Output
103+
32.400002
104+
a
105+
Test string
102106
```
103107

104108
The previous example illustrates these important concepts:
105109

106110
1. You must establish a list marker as a variable of type `va_list` before any variable arguments are accessed. In the previous example, the marker is called `vl`.
107-
108111
1. The individual arguments are accessed by using the `va_arg` macro. You must tell the `va_arg` macro the type of argument to retrieve so that it can transfer the correct number of bytes from the stack. If you specify an incorrect type of a size different from that supplied by the calling program to `va_arg`, the results are unpredictable.
109-
110112
1. You should explicitly cast the result obtained by using the `va_arg` macro to the type that you want.
111-
112-
You must call the macro to terminate variable-argument processing.`va_end`
113+
1. You must call the `va_end` macro to terminate variable-argument processing.

0 commit comments

Comments
 (0)