| <html lang="en"> |
| <head> |
| <title>Why Variadic - 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.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Variadic-Functions.html#Variadic-Functions" title="Variadic Functions"> |
| <link rel="next" href="How-Variadic.html#How-Variadic" title="How Variadic"> |
| <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.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 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.3 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 have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it 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> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Why-Variadic"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="How-Variadic.html#How-Variadic">How Variadic</a>, |
| Up: <a rel="up" accesskey="u" href="Variadic-Functions.html#Variadic-Functions">Variadic Functions</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">A.2.1 Why Variadic Functions are Used</h4> |
| |
| <p>Ordinary C functions take a fixed number of arguments. When you define |
| a function, you specify the data type for each argument. Every call to |
| the function should supply the expected number of arguments, with types |
| that can be converted to the specified ones. Thus, if the function |
| ‘<samp><span class="samp">foo</span></samp>’ is declared with <code>int foo (int, char *);</code> then you must |
| call it with two arguments, a number (any kind will do) and a string |
| pointer. |
| |
| <p>But some functions perform operations that can meaningfully accept an |
| unlimited number of arguments. |
| |
| <p>In some cases a function can handle any number of values by operating on |
| all of them as a block. For example, consider a function that allocates |
| a one-dimensional array with <code>malloc</code> to hold a specified set of |
| values. This operation makes sense for any number of values, as long as |
| the length of the array corresponds to that number. Without facilities |
| for variable arguments, you would have to define a separate function for |
| each possible array size. |
| |
| <p>The library function <code>printf</code> (see <a href="Formatted-Output.html#Formatted-Output">Formatted Output</a>) is an |
| example of another class of function where variable arguments are |
| useful. This function prints its arguments (which can vary in type as |
| well as number) under the control of a format template string. |
| |
| <p>These are good reasons to define a <dfn>variadic</dfn> function which can |
| handle as many arguments as the caller chooses to pass. |
| |
| <p>Some functions such as <code>open</code> take a fixed set of arguments, but |
| occasionally ignore the last few. Strict adherence to ISO C<!-- /@w --> requires |
| these functions to be defined as variadic; in practice, however, the GNU |
| C compiler and most other C compilers let you define such a function to |
| take a fixed set of arguments—the most it can ever use—and then only |
| <em>declare</em> the function as variadic (or not declare its arguments |
| at all!). |
| |
| </body></html> |
| |