Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to underline text using printf in C

I note the question Colorful text using printf in C gives a good example of setting coloured text on the standard console output in Windows. Is there something similar that allows output to be underlined? Or possibly even bolded or italicised?

EDIT: I tried Lundin's answer on using COMMON_LVB_UNDERSCORE with no luck. Attempting to use AddFontResource() to add arial italic font to try italics gives an error that there is an undefined reference to __imp_AddFontResourceA

like image 227
Toby Avatar asked Apr 01 '15 08:04

Toby


2 Answers

It is not possible to do so using any standard C functions, as the C language doesn't even recognize the presence of a screen.

With Windows API console functions you can change colors, underline and some other things. The particular function you are looking for is called SetConsoleTextAttribute just as in the post you linked. Change its attributes to include COMMON_LVB_UNDERSCORE.

like image 112
Lundin Avatar answered Nov 05 '22 21:11

Lundin


You might run your program in some environment with a terminal accepting ANSI escape codes.

(I never used Windows - since I am using Linux only -, so I don't know how to set up such environment in Windows; but I heard that it is possible)

With ANSI escape codes, underlining is "\e[4m" with \e being the ASCII ESCAPE character.

like image 2
Basile Starynkevitch Avatar answered Nov 05 '22 23:11

Basile Starynkevitch