hi all
could any 1 knows how to add color to printf?
i mean that i want to write a sentence with color in it.
thanx!

Recommended Answers

All 6 Replies

There's no portable way to do this. What compiler and operating system are you using? In anticipation of you being on Windows, you can use SetConsoleTextAttribute to give your text color.

im using Visual Studio and windows xp.

You're all set then. Follow the link I gave in my previous post for details.

10x!

This is for console printing. Before you printf something print this escape sequence to produce it in the desired color. There is a small routine textcolor() which does this automatically. You can use it in your programs along with the #define constants.

textcolor()

#includes <stdio.h>

#define RESET       0
#define BRIGHT      1
#define DIM     2
#define UNDERLINE   3
#define BLINK       4
#define REVERSE     7
#define HIDDEN      8

#define BLACK       0
#define RED     1
#define GREEN       2
#define YELLOW      3
#define BLUE        4
#define MAGENTA     5
#define CYAN        6
#define WHITE       7

void textcolor(int attr, int fg, int bg);
int main()
{   textcolor(BRIGHT, RED, BLACK);  
    printf("In color\n");
    textcolor(RESET, WHITE, BLACK); 
    return 0;
}

void textcolor(int attr, int fg, int bg)
{   char command[13];

    /* Command is the control command to the terminal */
    sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
    printf("%s", command);
}

The textcolor() is modeled against the Turbo C API function. You call the function to set the color and then print with a sprintf() (a function used in Turbo C to produce console output in color).
A Demo of colors

#include <stdio.h>

#define RESET       0
#define BRIGHT      1
#define DIM     2
#define UNDERLINE   3
#define BLINK       4
#define REVERSE     7
#define HIDDEN      8

#define BLACK       0
#define RED     1
#define GREEN       2
#define YELLOW      3
#define BLUE        4
#define MAGENTA     5
#define CYAN        6
#define WHITE       7

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

char *attrs[] = {"NORMAL", "BRIGHT", "DIM", "UNDERLINE", "BLINK",
         "REVERSE", "HIDDEN", "EXIT"};
char *colors[] = {"BLACK", "RED", "GREEN", "YELLOW", "BLUE", "MAGENTA",
         "CYAN", "WHITE", "EXIT"};
void textcolor(int attr, int fg, int bg);
int print_menu(char *array[], int n_options, char *title);
int main()
{   int attr, fg, bg;
    int attr_size, colors_size;

    attr_size = ARRAY_SIZE(attrs);
    colors_size = ARRAY_SIZE(colors);
    while(1)
    {   printf("\n");
        attr = print_menu(attrs, attr_size, "Choose the attr you want:");
        if(attr == attr_size - 1)
            break;
        fg = print_menu(colors, colors_size, "Choose the foreground you want:");
        if(attr == colors_size - 1)
            break;
        bg = print_menu(colors, colors_size, "Choose the background you want:");
        if(attr == colors_size - 1)
            break;
        printf("\n");
        textcolor(attr, fg, bg);    
        printf("This is what you get if you use the combination %s attribute %s foreground and %s
 background", attrs[attr], colors[fg], colors[bg]);
        textcolor(RESET, WHITE, BLACK);
        system("clear");
    }
    return 0;
}

int print_menu(char *array[], int n_options, char *title)
{   int choice, i;
    for(i = 0;i < n_options; ++i)
        printf("%d.%s\n", i, array[i]);
    printf("%s", title);
    scanf("%d", &choice);
    return choice;
}       
void textcolor(int attr, int fg, int bg)
{   char command[13];

    /* Command is the control command to the terminal */
    sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
    printf("%s", command);
}

This program asks the user to play with attributes and colors and shows a string in that color. You can use it to find out the best combination of colors for your GUIs.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.