C Variadic Functions

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Variadic Functions?

A variadic function takes a fixed parameter followed by .... Include <stdarg.h> and use va_list, va_start, va_arg, and va_end to read the extra arguments. printf is the best-known example. The function must know how many arguments there are and their types.

Remember
#include <stdarg.h>
int sum(int count, ...) {
    va_list ap;
    va_start(ap, count);
    int x = va_arg(ap, int);   /* repeat count times */
    va_end(ap);
}

Cheat Sheet

Keep these facts handy while you work with Variadic Functions in C.

TopicDetail
Startva_start(ap, last_fixed)
Fetchva_arg(ap, type)
Finishva_end(ap)
Knowing countPass a count or use a sentinel

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Inline Functions.

Example 1 — Sum a variable number of integers

The first argument says how many values follow.

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

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

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

int main(void) {
    printf("sum(3, 10, 20, 30) = %d\n", sum(3, 10, 20, 30));
    printf("sum(5, 1, 2, 3, 4, 5) = %d\n", sum(5, 1, 2, 3, 4, 5));
    return 0;
}

How It Works

  1. va_start initializes the list after the last named parameter.
  2. Each va_arg(ap, int) fetches the next argument.
  3. va_end cleans up before returning.

Edge Cases & Pitfalls

Check these before relying on Variadic Functions in real programs.

type

Wrong va_arg type

Reading a double as int is undefined behavior. Also remember float arguments are promoted to double and char to int.

count

Reading too many

Fetching more arguments than were passed is undefined.

Key Takeaways

  • Macros: va_start, va_arg, va_end.
  • Contract: caller and callee must agree on count and types.
  • Promotions: float to double, char to int.

One line: A variadic function takes a fixed parameter followed by ....

Frequently Asked Questions

It is a variadic function. It reads the format string to learn how many arguments and which types to fetch with va_arg.
No. The compiler cannot check the extra arguments against your expectations, except for special cases such as printf with format-attribute warnings.
Continue with "C Inline Functions" next. Suggest that the compiler expand a small function at its call site. The C Functions hub lists every topic in order.

Next: Inline Functions

Suggest that the compiler expand a small function at its call site.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful