How to highlight text (change color) in C ANSI [closed]

1

Good night ... I am in doubt about how to use the textcolor function in c but I did not find it in the library conio ... does anyone know where this function is located or if I have to create it how to do it? Thank you

    
asked by anonymous 11.10.2016 / 03:43

3 answers

1

If you are using the Operating System Windows , you can include the windows.h library and use:

typedef enum{BLACK,BLUE,GREEN,CYAN,RED,MAGENTA,BROWN,LIGHTGRAY,DARKGRAY,
LIGHTBLUE,LIGHTGREEN,LIGHTCYAN,LIGHTRED,LIGHTMAGENTA,YELLOW,WHITE} COLORS;

static int __BACKGROUND = BLACK;
static int __FOREGROUND = LIGHTGRAY;

void textcolor (int color)
{
    __FOREGROUND = color;
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
    color + (__BACKGROUND << 4));
}

You can also use the curses.h library:

start_color();      
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
printw("Mensagem com cor!");
attroff(COLOR_PAIR(1));
    
11.10.2016 / 16:02
1

Depending on the OS you use, you have different implementations. Systems that have MS-DOS have the conio.h library, however Unix has curses.h .

Another way is to directly write the characters responsible for the colors of your text (In the case of Unix systems):

Effects

/*****************************EFECTS***************************************/
#define NONE        "3[0m"
#define BOLD        "3[1m"
#define HALFBRIGHT  "3[2m"
#define UNDERSCORE  "3[4m"
#define BLINK       "3[5m"
#define REVERSE     "3[7m"

/*****************************COLORS***************************************/
#define C_BLACK     "3[30m"
#define C_RED       "3[31m"
#define C_GREEN     "3[32m"
#define C_YELLOW    "3[33m"
#define C_BLUE      "3[34m"
#define C_MAGENTA   "3[35m"
#define C_CYAN      "3[36m"
#define C_GRAY      "3[37m"

/***************************BACKGROUNDS************************************/
#define BG_BLACK    "3[40m"
#define BG_RED      "3[41m"
#define BG_GREEN    "3[42m"
#define BG_YELLOW   "3[43m"
#define BG_BLUE     "3[44m"
#define BG_MAGENTA  "3[45m"
#define BG_CYAN     "3[46m"
#define BG_GRAY     "3[47m"

These character combinations can be used to clear the screen, send closing signals, position the cursor, and more.

Using Effects

const char *string = "texto escrito em verde!";
printf("%s%s%s",C_GREEN,string,NONE);

The NONE serves to clear any stylization done, it returns the text style to the terminal pattern.

    
11.10.2016 / 05:36
-1

Depending on the Operating System, there is no implementation of conio.h , basically it depends on whether it was compiled in MS-DOS or not. Probably the system you're using was not.

In this case, use #include <curses.h> , which will give you almost all the features that conio.h . But if it is not installed, you may have to look for this file on the internet.

    
11.10.2016 / 04:20