c++ printf(char *str, arg 1, arg 2...etc)

Programming, for all ages and all languages.
Post Reply
slacker

c++ printf(char *str, arg 1, arg 2...etc)

Post by slacker »

how would i write a printf function that takes an unlmited number of arguments like the printf function

printf(str, %c,%i)

also i dont want to use any standard functions like stdarg.h
Tim

Re:c++ printf(char *str, arg 1, arg 2...etc)

Post by Tim »

slacker wrote: how would i write a printf function that takes an unlmited number of arguments like the printf function

printf(str, %c,%i)

Code: Select all

int printf(const char *format, ...);
also i dont want to use any standard functions like stdarg.h
You've got to use stdarg.h, or at least copy the code out of it. stdarg.h defines the methods of accessing a variable number of parameters for your particular platform. stdarg.h will work on your processor regardless of the OS.
LOneWoolF

Re:c++ printf(char *str, arg 1, arg 2...etc)

Post by LOneWoolF »

Hey slacker

have a look at stdarg

an easy way is:

(if you use GCC)

Code: Select all

#define va_list         __builtin_va_list
#define va_start(v, l)  __builtin_stdarg_start((v), l)
#define va_end          __builtin_va_end
#define va_arg          __builtin_va_arg
#define va_copy(d,s)    __builtin_va_copy((d), (s))

and in the function

Code: Select all

va_list ap;
 va_start(ap, fmt);
(loop through that arguments...)
example:
 char *s = va_arg(ap, char *); //if iz a character *
 int i = va_arg(ap, int); // if iz an int
(...)
 va_end(ap);your function:
the second argument of va_start identifies the last argument before the ... of the function (i think)

go and get a look on the printf function and the vsprintf function of the first public linux kernel available on
osdev.neopages.net (download section)

GreeZ
LOneWoolF
Post Reply