cprintf
Writes styled formatted text to stdout.
📝Syntax
cprintf()
cprintf(format, v1, ... , vn)
cprintf(style, format, v1, ... , vn)
R = cprintf(style, format, v1, ... , vn)
📥Input Arguments
Parameter Description
style a text style name, RGB vector, or hexadecimal color.
format a string describing the text format.
v1, ... , vn data to convert and print according to the format parameter.
📤Output Arguments
Parameter Description
R number of visible characters written to stdout.
📄Description

cprintf writes formatted text to stdout and applies a text style when the current interface supports styled output. The basic CLI displays plain text; advanced CLI and GUI can render styles.

Without a style argument, cprintf behaves like a stdout formatted print using plain text. With no input argument, it displays a short style demo.

The format argument follows the same formatting rules as fprintf.

Named styles include text, keywords, comments, strings, unterminatedstrings, systemcommands, errors, hyperlinks, and common color names such as black, blue, cyan, green, magenta, red, yellow, white, gray, orange, pink, purple, brown, and gold.

Named styles and colors accept unambiguous prefixes. Light and dark variants can be written with a light or dark prefix, for example lightblue or dark-green.

RGB colors can be specified as a three-element vector in the range [0, 1] or [0, 255]. Hexadecimal colors can use #RGB or #RRGGBB.

A leading * requests bold text. A leading _ or - requests underlined text. Prefixes can be combined in either order, for example *_red or _*#0af.

Limitations: rendering depends on the terminal or GUI capabilities; the basic CLI and interfaces without style support display plain text. evalc captures only visible text. Files and diary output do not preserve style information. Output is always sent to stdout, never to stderr.

💡Examples
Short demo
cprintf()
Plain formatted text
cprintf('value: %.3f\n', pi)
Named colors
cprintf('red', 'red text\n')
cprintf('blue', 'blue text\n')
cprintf('cy', 'cyan from an unambiguous prefix\n')
Light and dark variants
cprintf('lightgreen', 'light green\n')
cprintf('dark-blue', 'dark blue\n')
RGB colors
cprintf([1 0.4 0], 'normalized RGB\n')
cprintf([0 128 255], 'byte RGB\n')
Hexadecimal colors
cprintf('#0af', 'short hex\n')
cprintf('#00aaff', 'long hex\n')
Bold and underline
cprintf('*red', 'bold red\n')
cprintf('_green', 'underlined green\n')
cprintf('*_dark-magenta', 'bold underlined dark magenta\n')
Multiple lines, Unicode, and count
msg = ['accent ', char(233)];
count = cprintf('comments', 'line 1\n%s\n', msg)
Capture visible text
txt = evalc('cprintf(''red'', ''captured'')')
🔗See Also
fprintfdiary
🕔Version History
Version Description
2.0.0 initial version
Edit this page on GitHub