|
|
| Embedded Systems Building Blocks: Complete and Ready-To-Use Modules in C (ISBN: 0879306041) |
 |
List Price: $89.95
Our Price: $62.97
Used Price: $39.00
Release Date: February, 2000
Manufacturer: CMP Books (Hardcover)
Sales Rank: 14,998
Author: Jean J. Labrosse
|
More Info
|
|
|
Variable Argument List Example
|
2003-01-27 23:29:38
|
| |
c
|
|
|
|
|
Category: source: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.
|
|
Platform: all
|
|
Author: mind
|
|
Viewed: 13875
|
|
Rating: 3.9/5 (82 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
/* 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 );
}
|
|
|