Variable Argument List Example
2003-01-27 23:29:38
Category: c:general
Description: Somewhat simple example on creating your own print and how to use va_list, va_arg, etc.. Using these functions, you can do more than just create your own print, You can use this example to create your function using strings, integers, long integers, etc.. Enjoy.
Author: mind
Viewed: 14766
Rating: (84 votes)


/* va_arg.c by mind [mind@metalshell.com]
 *
 * Somewhat simple example on creating your own print
 * and how to use va_list, va_arg, etc.. Using these
 * functions, you can do more than just create your own
 * print, You can use this example to create your function
 * using strings, integers, long integers, etc.. Enjoy.
 *
 * 01/27/03
 *
 */
 
#include <stdio.h>
#include <stdarg.h>
 
#define exmpl   "test"
#define exm     10
 
void eprintf( char *fmt, ... );
 
int main()
{
        eprintf("Example #1 of va_arg() - %s\n", exmpl);
        eprintf("Example #2 of va_arg() - %i\n", exm);
        return 1;
}
 
 
/* eprintf( format, args ) */
void eprintf( char *fmt, ... )
{
        va_list         list;
        char            *p, *r;
        int             e;
 
        /* prepare list for va_arg */
        va_start( list, fmt );
 
        for ( p = fmt ; *p ; ++p )
        {
                /* check if we should later look for *
                 * i (integer) or s (string) */
                if ( *p != '%' )
                {
                        /* not a string or integer print *
                         * the character to stdout */
                        putc( *p, stdout );
                } else {
                        /* character was % so check the *
                         * letter after it and see if it's *
                         * one of s or i */
                        switch ( *++p )
                        {
                                /* string */
                                case 's':
                                {
                                        /* set r as the next char *
                                         * in list (string) */
                                        r = va_arg( list, char * );
 
                                        /* print results to stdout */
                                        printf("%s", r);
                                        continue;
                                }
 
                                /* integer */
                                case 'i':
                                {
                                        /* set e as the next char *
                                         * in list (integer) */
                                        e = va_arg( list, int );
 
                                        /* print results to stdout */
                                        printf("%i", e);
                                        continue;
                                }
 
                                default: putc( *p, stdout );
                        }
                }
        }
        va_end( list );
        fflush( stdout );
}