C stdarg.h Header

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is stdarg.h?

<stdarg.h> lets you write functions with a variable number of arguments using ... (ellipsis)—the same idea behind printf. You walk the list with va_list, va_start, va_arg, and va_end.

Remember
#include <stdarg.h>

int f(int count, ...) {   // fixed args, then ...
    va_list ap;
    va_start(ap, count);  // last fixed parameter
    x = va_arg(ap, int);  // next arg (you choose the type)
    va_end(ap);
}

Need a way to know how many / which types:
  count · format string · NULL sentinel
UsePrefer
Flexible arity (sum, log)stdarg.h macros
Printf-style formattingvprintf / format string
Fixed, type-safe APIsArrays, structs, or normal parameters

How to Use stdarg.h in C

Declare ..., start a va_list, pull each argument by type, then end the list.

  1. Add #include <stdarg.h> and write a function with at least one fixed parameter before ....
  2. Declare va_list ap; and call va_start(ap, last_fixed).
  3. Loop with va_arg(ap, type) using a count, format, or sentinel to know when to stop.
  4. Always call va_end(ap) before returning.
PieceRequired?Role
va_listYesCursor into the variable argument list
va_start / va_endYesOpen and close the list
va_arg(ap, type)YesRead the next argument as type

Pseudocode

Pseudocode
function sum(count, ...):
    ap = empty va_list
    va_start(ap, count)
    total = 0
    repeat count times:
        total += va_arg(ap, int)
    va_end(ap)
    return total

// caller: sum(3, 3, 5, 7) → 15

Cheat Sheet

Keep these facts handy while writing variadic functions.

TopicDetail
Header#include <stdarg.h>
Declarationreturn_type name(fixed, ...);
Start / endva_start(ap, last_fixed) → va_end(ap)
Fetchva_arg(ap, type)
Promotionsfloat→double; char/short→int
Stop conditionsCount, format string, or sentinel (NULL)
Sample checksum(3, 3, 5, 7) → 15

Live Preview

Simulate a count-based sum(...) call—same idea as va_start / va_arg / va_end below.

Browser simulation of variadic walking (same idea as the C examples below).

Result
va_start(ap, count)   // count = 3
va_arg → 3
va_arg → 5
va_arg → 7
va_end(ap)
sum = 15

Worked Walkthrough — sum(3, 3, 5, 7)

Trace one count-based sum: three integers after the count become 15.

Calculation
Call: sum(3, 3, 5, 7)

count = 3                 // fixed parameter
va_start(args, count)     // list starts after count

i=0: va_arg → 3  → total = 3
i=1: va_arg → 5  → total = 8
i=2: va_arg → 7  → total = 15

va_end(args)
return 15 ✓

C Examples

Compile with gcc file.c -std=c11 -o out. The compiler does not type-check ...—your convention must match the callers.

Example 1 — Sum Integers with a Count

Classic pattern: the first argument says how many int values follow.

C
#include <stdarg.h>
#include <stdio.h>

int sum(int count, ...) {
    int total = 0;
    va_list args;
    int i;

    va_start(args, count);
    for (i = 0; i < count; i++) {
        total += va_arg(args, int);
    }
    va_end(args);

    return total;
}

int main(void) {
    printf("Sum of 3, 5, 7: %d\n", sum(3, 3, 5, 7));
    printf("Sum of 1..5: %d\n", sum(5, 1, 2, 3, 4, 5));

    return 0;
}

How It Works

  1. Anchor on the fixed arg. va_start(args, count) uses count to find where ... begins.
  2. Loop exactly count times. Each va_arg(args, int) pulls the next integer.
  3. Add into total. 3 + 5 + 7 = 15 for the first call.
  4. Clean up. va_end(args) finishes the list before returning.

Example 2 — Average of double Values

Same count pattern with floating values. Remember: float arguments promote to double in ....

C
#include <stdarg.h>
#include <stdio.h>

double average(int count, ...) {
    va_list args;
    double sum = 0.0;
    int i;

    va_start(args, count);
    for (i = 0; i < count; i++) {
        sum += va_arg(args, double);
    }
    va_end(args);

    return count > 0 ? sum / count : 0.0;
}

int main(void) {
    printf("Avg: %.2f\n", average(4, 10.0, 20.0, 30.0, 40.0));
    return 0;
}

How It Works

  1. Pass doubles. Literals like 10.0 match va_arg(args, double).
  2. Accumulate. Four values sum to 100.0.
  3. Divide by count. 100 / 4 = 25.00.
  4. Promotion tip. If a caller passes a bare float, still read with double—never va_arg(ap, float).

Example 3 — Tiny Log Wrapper with vprintf

Printf-style API: a level string plus a format. Delegate the va_list to vprintf.

C
#include <stdarg.h>
#include <stdio.h>

void debug_log(const char *level, const char *fmt, ...) {
    va_list args;

    printf("[%s] ", level);
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
    printf("\n");
}

int main(void) {
    debug_log("INFO", "User %s logged in (id=%d)", "Alice", 42);
    debug_log("WARN", "Retries left: %d", 3);

    return 0;
}

How It Works

  1. Print the level. A normal printf writes [INFO] or [WARN].
  2. Open the list. va_start(args, fmt) anchors on the last fixed parameter (fmt).
  3. Forward to vprintf. The format string tells vprintf which types to read from args.
  4. Close the list. va_end(args) cleans up after the formatted message.

Edge Cases & Pitfalls

Check these before shipping variadic APIs.

types

Wrong va_arg type is undefined

Reading an int as a double (or the reverse) is undefined behavior. Match the caller’s actual types.

promote

Respect default promotions

Use va_arg(ap, double) for floating values and va_arg(ap, int) for small integers—not float or char.

count

Never over-read the list

Calling va_arg more times than there are arguments is undefined. Trust a count, format, or sentinel.

cleanup

Always call va_end

Pair every va_start with va_end. To walk the list twice, use C99 va_copy.

Performance Notes

ApproachCostBest for
va_arg loopCheap per argumentSmall N (logs, sums)
Array / pointer + lengthOften clearer / inlinableHot numeric code
vprintf wrappersDominated by formatting I/ODebug / logging APIs

Variadic macros are rarely a bottleneck. Prefer fixed parameters or arrays when types matter more than flexible arity.

Key Takeaways

  • Role: read ... args with va_list macros.
  • Lifecycle: va_start → va_arg → va_end.
  • Convention: count, format, or sentinel—you choose.
  • Types: match callers; respect promotions.

One line: stdarg.h walks the ... list—but only you know how many args and which types are there.

Frequently Asked Questions

stdarg.h provides macros to read variable arguments in functions declared with ... (ellipsis). va_start initializes a va_list, va_arg fetches each argument by type, and va_end cleans up. printf and scanf use this machinery internally.
va_start(ap, last) needs the name of the last fixed parameter before the ... arguments. The implementation uses it to find where the variable args begin. It must be the parameter immediately before ...
printf reads the format string (%d, %f, %s) to decide which type to pass to va_arg each time. Your functions must use a similar convention—pass a count, a format string, or a sentinel—because the compiler does not type-check ... arguments.
In variable argument lists, char and short are promoted to int, and float is promoted to double. Always use va_arg(ap, int) for promoted char/short and va_arg(ap, double) for float values passed through ...
va_list is a type (typedef), not a macro. va_start, va_arg, va_end, and va_copy (C99) are macros. You declare va_list ap; then call va_start(ap, last_fixed_arg).
Use them for logging, printf-style APIs, or wrappers where arity varies. For most application code, fixed parameters, arrays, or structs are safer. Prefer stdarg when you truly need flexible arity.

Next: stdbool.h

Use proper boolean types with bool, true, and false.

stdbool.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful