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
c++ printf(char *str, arg 1, arg 2...etc)
Re:c++ printf(char *str, arg 1, arg 2...etc)
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, ...);
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.also i dont want to use any standard functions like stdarg.h
Re:c++ printf(char *str, arg 1, arg 2...etc)
Hey slacker
have a look at stdarg
an easy way is:
(if you use GCC)
and in the 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
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:
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