-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalling-Variadics.html
More file actions
96 lines (84 loc) · 4.97 KB
/
Calling-Variadics.html
File metadata and controls
96 lines (84 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<html lang="en">
<head>
<title>Calling Variadics - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.9">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="How-Variadic.html#How-Variadic" title="How Variadic">
<link rel="prev" href="How-Many-Arguments.html#How-Many-Arguments" title="How Many Arguments">
<link rel="next" href="Argument-Macros.html#Argument-Macros" title="Argument Macros">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.11, last updated 2007-09-09,
of `The GNU C Library Reference Manual', for version 2.7.
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You are free to copy and modify
this GNU Manual. Buying copies from GNU Press supports the FSF in
developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Calling-Variadics"></a>
Next: <a rel="next" accesskey="n" href="Argument-Macros.html#Argument-Macros">Argument Macros</a>,
Previous: <a rel="previous" accesskey="p" href="How-Many-Arguments.html#How-Many-Arguments">How Many Arguments</a>,
Up: <a rel="up" accesskey="u" href="How-Variadic.html#How-Variadic">How Variadic</a>
<hr>
</div>
<h5 class="subsubsection">A.2.2.4 Calling Variadic Functions</h5>
<p><a name="index-variadic-functions_002c-calling-3720"></a><a name="index-calling-variadic-functions-3721"></a><a name="index-declaring-variadic-functions-3722"></a>
You don't have to do anything special to call a variadic function.
Just put the arguments (required arguments, followed by optional ones)
inside parentheses, separated by commas, as usual. But you must declare
the function with a prototype and know how the argument values are converted.
<p>In principle, functions that are <em>defined</em> to be variadic must also
be <em>declared</em> to be variadic using a function prototype whenever
you call them. (See <a href="Variadic-Prototypes.html#Variadic-Prototypes">Variadic Prototypes</a>, for how.) This is because
some C compilers use a different calling convention to pass the same set
of argument values to a function depending on whether that function
takes variable arguments or fixed arguments.
<p>In practice, the GNU C compiler always passes a given set of argument
types in the same way regardless of whether they are optional or
required. So, as long as the argument types are self-promoting, you can
safely omit declaring them. Usually it is a good idea to declare the
argument types for variadic functions, and indeed for all functions.
But there are a few functions which it is extremely convenient not to
have to declare as variadic—for example, <code>open</code> and
<code>printf</code>.
<p><a name="index-default-argument-promotions-3723"></a><a name="index-argument-promotion-3724"></a>Since the prototype doesn't specify types for optional arguments, in a
call to a variadic function the <dfn>default argument promotions</dfn> are
performed on the optional argument values. This means the objects of
type <code>char</code> or <code>short int</code><!-- /@w --> (whether signed or not) are
promoted to either <code>int</code> or <code>unsigned int</code><!-- /@w -->, as
appropriate; and that objects of type <code>float</code> are promoted to type
<code>double</code>. So, if the caller passes a <code>char</code> as an optional
argument, it is promoted to an <code>int</code>, and the function can access
it with <code>va_arg (</code><var>ap</var><code>, int)</code>.
<p>Conversion of the required arguments is controlled by the function
prototype in the usual way: the argument expression is converted to the
declared argument type as if it were being assigned to a variable of
that type.
</body></html>