6

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

Community
  • 1
  • 1
Toby
  • 9,696
  • 16
  • 68
  • 132

4 Answers4

3

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • No luck with that on my win7 system, with that option turned on, all the printf() string chars are printed as spaces, except new-lines :( – Toby Apr 01 '15 at 09:17
  • 1
    @Toby Hmm, so it seems. Been ages since I used these functions. I'll do some research, see if I can find out why. – Lundin Apr 01 '15 at 09:44
  • 1
    The `COMMON_LVB_*` attributes are only used for [`CHAR_INFO`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682013%28v=vs.85%29.aspx) attributes when the console is using a [DBCS codepage](https://msdn.microsoft.com/en-us/library/windows/desktop/dd317794%28v=vs.85%29.aspx). – Eryk Sun Apr 01 '15 at 21:53
2

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Perhaps try using termcaps. Something like this (after initializing termcaps) :

printf(tgetstr("us", NULL)); /* underline on */
printf(""/* your string */);
printf(tgetstr("ue", NULL)); /* underline off */

or more concise :

printf("%s/* your text here */%s", tgetstr("us", NULL), tgetstr("ue", NULL));

https://www.gnu.org/software/termutils/manual/termcap-1.3/html_node/termcap_34.html

Boiethios
  • 38,438
  • 19
  • 134
  • 183
-2

The '\r' sequence differs from the newline ('\n') in that it moves the cursor to the beginning of the current line of output, not to the beginning of the next line. Using '\r' gives a program the ability to create a file containing more than one character in one line position.

This prints an underlined text printf("\f\t\t\tFinal Report\r\t\t\t____________\n");

Warren
  • 1