cprintf(format, ...)
cfprintf(stream, format, ...)
csprintf(string, format, ...)
scprintf(format, ...)
scfprintf(stream, format, ...)
cprintf is a enhanced version of printf() family of functions. It provides the following additional features:
cprintf supports inline ANSI color formatting by parsing color tags:
- {color} sets the foreground color
- [color] sets the background color
Supported colors are:
clear :reset all colors
underline :underline text
highlight :highlight text
red, blue, green, yellow, cyan, black, white, purple
base :user-defined color
R;G;B :RGB color (e.g., 255;0;0 for red)
cprintf("This is {red}red{clear} and this is [blue]blue[clear].\n");This feature is not supported in csprintf().
cprintf supports generic type formatting:
- T(var) Automatically detects the type of
varand formats it. - F(var, "format") Formats
varusing the specified format string.
int x = 42;
cprintf("Value: {}", T(x)); // Automatically detects type and format x
cprintf("Value: {}", F(x, "08")); // Formats using specified format %08dThis feature is not supported in scprintf() and scfprintf().
The generic formatting feature converts every {} to %s, which disables compile-time format string checking and may lead to undefined behavior if not used carefully.
Color output is automatically disabled if the output is not a character device (e.g., redirected to a file).
This behavior can be overridden by setting the global flag:
cprintf_print_color_only_tty = false;The output color can be customized by setting the global variable:
struct CPRINTF_COLOR__ cprintf_color = {
.base = "254;228;208",
.black_fg = "\033[30m",
.red_fg = "\033[31m",
.green_fg = "\033[32m",
.yellow_fg = "\033[33m",
.blue_fg = "\033[34m",
.purple_fg = "\033[35m",
.cyan_fg = "\033[36m",
.white_fg = "\033[37m",
.black_bg = "\033[40m",
.red_bg = "\033[41m",
.green_bg = "\033[42m",
.yellow_bg = "\033[43m",
.blue_bg = "\033[44m",
.purple_bg = "\033[45m",
.cyan_bg = "\033[46m",
.white_bg = "\033[47m",
};cprintf is not thread-safe.