YOUR C LEARNING COMPANION

Find your next idea.

Learning path
Building programsChapter 47 of 73

Variadic functions: arguments need an agreement

Understand count and type contracts, default argument promotions, va_list traversal, cleanup, and safer interface choices.

5 min readDevelopingC17 baseline
Offline reading
In this chapter8 sections

A normal prototype gives each parameter a type. A variadic function also accepts trailing arguments whose types are not listed individually. This flexibility works only when caller and function share a precise agreement about how many arguments exist and what types they have.

1. The dots permit arguments; they do not describe them

In largest(unsigned count, ...), count is a fixed parameter. The ellipsis permits additional arguments. It does not provide portable runtime metadata describing their number or types. Our interface chooses count as the number of trailing unsigned int values. Other interfaces use a format or a sentinel, each with its own risks.

Compare printf("%d", 4): the format tells printf which kind of value to retrieve. The text is not a request to convert any arbitrary supplied type to int. A mismatch can violate the call contract even when the printed result looks plausible.

2. Follow the traversal state

Complete C17 program
#include <assert.h>
#include <stdarg.h>

/* Exactly count trailing arguments, each of type unsigned int.
   The application chooses zero for an empty list. */
static unsigned largest(unsigned count, ...)
{
    va_list arguments;
    va_start(arguments, count);
    unsigned best = 0U;
    for (unsigned i = 0U; i < count; ++i) {
        unsigned next = va_arg(arguments, unsigned int);
        if (next > best) best = next;
    }
    va_end(arguments);
    return best;
}

int main(void)
{
    assert(largest(0U) == 0U);
    assert(largest(1U, 7U) == 7U);
    assert(largest(3U, 4U, 9U, 2U) == 9U);
    assert(largest(3U, 0U, 0U, 0U) == 0U);
    return 0;
}

va_list holds traversal state. va_start initialises it, each va_arg retrieves one argument, and va_end finishes that traversal. largest retrieves exactly count arguments. Calling it with too few arguments lets it read beyond the supplied list and is invalid. Extra flexibility does not add automatic bounds checks.

3. Retrieve the promoted type

Trailing arguments undergo default argument promotions. A float is promoted to double. Small integer types undergo integer promotions, commonly to int, or to unsigned int when required by their range. These rules are different from converting an argument to a fixed parameter’s declared type.

A function expecting a promoted floating argument retrieves double with va_arg, not float. In our example, the U suffix makes the small supplied constants unsigned int, matching the retrieval type. As a teaching rule, supply and retrieve the matching promoted type; do not depend on the standard’s narrow mismatch exceptions.

4. Copy traversal state through va_copy

A va_list is not a universally simple pointer or integer. Do not copy it with ordinary assignment as a portable way to create an independent traversal. Use va_copy, and pair each initialisation with va_end in the same function. After another function traverses a passed list, follow the specified state rules before trying to reuse it.

For the C17 two-argument va_start form, the last named parameter has restrictions. In particular, do not choose a float parameter, a register parameter, or a parameter whose declared type is incompatible with its promoted type. The unsigned count parameter used above avoids those problems.

5. Prefer a more explicit interface when possible

For an ordinary sequence of values of one type, a pointer and count often express the contract more clearly. A structure can name different fields. Variadic APIs are useful for particular interface needs, but they move important checks away from the ordinary prototype. Learn the mechanism without treating it as the default way to accept a list.

Think it through

A float is supplied after the ellipsis. Should the function retrieve it with va_arg(arguments, float)?

Read the explanation

No. The default argument promotions produce double, so the matching retrieval type is double. The format or other agreement must describe that promoted argument.

Primary references and further reading

Before you move on

  • Trailing variadic arguments need an explicit count and type agreement.
  • Default argument promotions affect which type va_arg must retrieve.
  • Each va_start or va_copy traversal needs the corresponding va_end.
Keep these ideas separate
Format request / Argument conversion
A format describes what the callee expects; it does not generally convert an incorrectly supplied variadic type.
Explain it in your own words

Why does a float passed after the ellipsis need to be retrieved as double?

Optional: say it aloud or keep a note. You can continue whenever you are ready.

Check your understanding

Optional. Answers are not saved or sent, and reading stays unrestricted.

A float value is passed through ... in a prototyped variadic call. What type does its default argument promotion supply?
Read the explanation for every choice
  1. float, with no change.

    Default argument promotions apply to arguments corresponding to the ellipsis.

  2. double.

    ✓ Correct reasoning. The receiving variadic code must follow the promoted-type agreement, not retrieve it as float.

  3. A pointer to the caller’s float object.

    This is a value conversion, not an implicit address-taking operation.

C17

Ready for the next idea?

Mark this chapter as read whenever you feel ready.

CHAPTER 47 / 73

In this chapter

Variadic functions: arguments need an agreement

Search all 73 chapters in both languages.EN + HG

Try “pointer”, “why int”, or “undefined behavior”.

Concepts connect. Follow your curiosity.↑ ↓ to explore · Enter to open
MAKE YOURSELF COMFORTABLE

Your reading space

Appearance

A clear canvas with balanced contrast.

Text size

100%
AA

Reading tools

Your data stays in this browser

Export a backup before changing browsers or replacing an older version of the reader.

YOUR PERSONAL MARGINS

My notebook

Saved on this device
A LITTLE CLARITY

The glossary

A starting meaning, then a chapter to go deeper.

A QUICK DEFINITION

A little clarity

Read the full explanation →

A thought worth keeping