When debugging a program, sometimes a large amount of data needs to be output. If you want printf/fprintf to change the color of the output data, it is much easier to observe the data.
The Character Color of the terminal is controlled by escape sequence. It is the system display function in text mode and has nothing to do with the specific language.
The escape sequence starts with ESC and can be used to complete the same work with \ 033 (the ASCII code of ESC is expressed in decimal format as 27, = 33 in octal format ).
\ 033 [display mode; foreground color; background color m
Display Mode: 0 (default), 1 (highlighted), 22 (not bold), 4 (underline), 24 (non-underline), 5 (blinking), 25 (non-blinking), 7 (reverse display), 27 (non-reverse display)
Foreground color: 30 (black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (magenta), 36 (blue), 37 (white)
Background Color: 40 (black), 41 (red), 42 (green), 43 (yellow), 44 (blue), 45 (Foreign red), 46 (blue), 47 (white)
\ 033 [0 m default
\ 033 [1; 32; 40 m green
\ 033 [1; 31; 40 m red
Printf ("\ 033 [1; 31; 40 m output red character \ 033 [0 m ")
When debugging a program, sometimes a large amount of data needs to be output. If the color is the same, it will be annoying to identify it,
If you want printf/fprintf to change the color of the output data, it is much easier to observe the data.
Sample Code:
# Include <stdio. h>
# Define none "\ 033 [M"
# Define red "\ 033 [0; 32; 31 m"
# Define light_red "\ 033 [1; 31 m"
# Define green "\ 033 [0; 32; 32 m"
# Define light_green "\ 033 [1; 32 m"
# Define blue "\ 033 [0; 32; 34 m"
# Define light_blue "\ 033 [1; 34 m"
# Define dary_gray "\ 033 [1; 30 m"
# Define cyan "\ 033 [0; 36 m"
# Define light_cyan "\ 033 [1; 36 m"
# Define purple "\ 033 [0; 35 m"
# Define light_purple "\ 033 [1; 35 m"
# Define Brown "\ 033 [0; 33 m"
# Define yellow "\ 033 [1; 33 m"
# Define light_gray "\ 033 [0; 37 m"
# Define white "\ 033 [1; 37 m"
Int main ()
{
Printf (cyan "current function is % s" green "file line is % d \ n" none,
_ FUNCTION __, _ line __);
Fprintf (stderr, red "current function is % s" blue "file line is % d \ n" none,
_ FUNCTION __, _ line __);
Return 0;
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/dedodong/archive/2007/07/02/1675528.aspx