Search code examples
cconsoleprintfcurses

A function with undefined argument numbers which call printf()


Im trying to create a function which call printf (printw in my case im using curses) and the reason why im doing this because i want to pass the color and do the refresh in the same function so instead of writing 3 lines everytime i wwant to show something i only have to do it once with one function so the function im going to create look like this in C :

void outputConsole(int color_id, const char* a, ...)
{
    attron(COLOR_PAIR(2));
    printw(a,...);
    refresh();
}

Solution

  • You can do that using vw_printw. Solution is

    void outputConsole(int color_id, const char* a, ...)
    {
        attron(COLOR_PAIR(2));
        va_list args;
        va_start(args, a);
        vw_printw(stdscr, a, args);
        va_end(args);
        refresh();
    }
    

    Also while declaring to ensure compiler format string checking you can write like this

    void outputConsole(int color_id, const char* a, ...)
    #ifdef __GNUC__ 
           __attribute__(( format (printf, 2, 3)));
    #else 
           ;
    #endif