콘솔 글자색, 배경색 수정(2) - Win, Linux
Window11
Linux
C언어
콘솔
출력 색
터미널에 출력 문자, 배경 색 변경 방법
중요한 로그(디버깅시 오류 등)를 보기 쉽게 컬러로 출력 가능
① ANSI Color 코드
ANSI 컬러 코드 + 문자 + 뒤에 ANSI Reset 코드
예제) printf("\x1b[30m log text \x1b[0m \n");
예제) printf("\x1b[30m" "%s" "\x1b[0m \n");
② 사용법
②.①
#include <stdio.h>
#define ANSI_COLOR_RED "\x1b[30m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
#define ansi_bg_black "\x1b[40m"
#define ansi_bg_red "\x1b[41m"
#define ansi_bg_green "\x1b[42m"
#define ansi_bg_yellow "\x1b[43m"
#define ansi_bg_blue "\x1b[44m"
#define ansi_bg_magenta "\x1b[45m"
#define ansi_bg_cyan "\x1b[46m"
#define ansi_bg_white "\x1b[47m"
int main()
{
printf(ANSI_COLOR_YELLOW " *\n");
printf(ANSI_COLOR_GREEN " * *\n");
printf(ANSI_COLOR_RED " * * *\n");
printf(ANSI_COLOR_GREEN "* * * *\n"); printf(ANSI_COLOR_RESET);
printf(" ### \n");
printf(" ### \n");
printf(" ### \n");
printf("\n");
printf("\n");
char str[3][4] = { "★", "☆", "★"};
printf("1. 특수 문자 출력");
printf("\n");
for (size_t i = 0; i < 3; i++)
{
printf(ANSI_COLOR_GREEN "%s " ANSI_COLOR_RESET, str[i]);
printf(ansi_bg_green "%s " ansi_bg_black
, str[i]);
printf("\n");
}
return 0;
}
②.②
#include <stdio.h>
int main(void)
{
int i, j, n;
for (i = 0; i < 11; i++) {
for (j = 0; j < 10; j++) {
n = 10 * i + j;
if (n > 108) break;
printf("\033[%dm %3d\033[m", n, n);
}
printf("\n");
}
return 0;
}