Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdarg.h>
- size_t vmputs(size_t count, va_list arg_list);
- // mputs ("multiple" puts)
- // like puts, but for multiple strings.
- size_t mputs(size_t count, ...) {
- va_list arg_list;
- va_start(arg_list, count);
- size_t rc = vmputs(count, arg_list);
- // I could implement this directly here
- // but then I would be duplicating the code from vmputs
- va_end(arg_list);
- return rc;
- }
- // as is convention
- // for every variadic function,
- // I have a second one prefixed with a 'v' that takes a va_args
- size_t vmputs(size_t count, va_list arg_list) {
- size_t s = 0;
- for(size_t i = 0; i < count; ++i) {
- char* arg = va_arg(arg_list, char*);
- s += puts(arg);
- }
- return s;
- }
- // dbg_printf is like printf, but prepends "debug message: "
- // The "v" convention is found in most std functions
- // including the *printf family
- // so I can do things like this
- size_t dbg_printf(char* fmt, ...) {
- size_t s = 0;
- s += printf("debug message: ");
- // how do I pass all of my ... to printf??
- // use vprintf!
- va_list arg_list;
- va_start(arg_list, fmt);
- s += vprintf(fmt, arg_list);
- va_end(arg_list);
- return s;
- }
- int main() {
- mputs(3, "hello", "world", ".");
- dbg_printf("%d != %d\n", 3, 5);
- return 0;
- }
- // Output:
- // hello
- // world
- // .
- // debug message: 3 != 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement