1

I am making a C library that creates a print function, which basically executes printf. Because of this, I wish to create a duplicate of printf from glibc, but with the name print. How can I duplicate this function without duplicating all of it's code?

(I found the code here but don't understand how to duplicate it in my library, or if it is legal to do so.)

Community
  • 1
  • 1
Ethan McTague
  • 2,236
  • 3
  • 21
  • 53
  • 2
    The initial question was `println` with `'\n'`, now you changed that one day after and all the answers look wrong!. – ouah Jul 30 '14 at 14:06
  • I think the question was always intended to mean what it says now, but the original wording was confusing. Lots of people appear to have misunderstood it. – dohashi Jul 30 '14 at 19:45

3 Answers3

3

There you go:

#include <stdarg.h>

void println(const char* format,...)
{
    va_list args;
    va_start(args,format);
    vprintf(format,args);
    printf("\n");
    va_end(args);
}
barak manos
  • 29,648
  • 10
  • 62
  • 114
3

You can use a variadic macro:

#define println(...)  (printf(__VA_ARGS__), (void) puts(""))
ouah
  • 142,963
  • 15
  • 272
  • 331
2

You can either use a Macro:

#define print  printf

or define a wrapper function

int print( char *fmt, ... )
{
     va_list ap;
     int n;

     va_start(ap, fmt);
     n = vprintf(fmt, ap);
     va_end(ap);

     return n;
}
dohashi
  • 1,771
  • 8
  • 12