A simple "die" function for C - example of variable number of arguments

This page was created on 2010-07-11.

This simple function illustrates receiving and sending out a variable number of arguments in C using <stdarg.h>.

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

static void die (int line_number, const char * format, ...)
{
    va_list vargs;
    va_start (vargs, format);
    fprintf (stderr, "%d: ", line_number);
    vfprintf (stderr, format, vargs);
    fprintf (stderr, ".\n");
    exit (1);
}

Copyright © Ben Bullock 2009-2010. All rights reserved. For comments, questions, and corrections, please email Ben Bullock/ Privacy/ Disclaimer