How to underline text using printf in C - 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

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.

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.

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

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");

Related

Can I change the text color through sprintf in C?

I'm new to C and I came across this code and it was confusing me:
sprintf(banner1, "\e[37╔═╗\e[37┌─┐\e[37┌┐┌\e[37┌─┐\e[37┌─┐\e[37┌─┐\e[37┌─┐\e[37m\r\n");
sprintf(banner2, "\e[37╠═╝\e[37├─┤\e[37│││\e[37│ ┬\e[37├─┤\e[37├┤\e[37 ├─┤\e[37m\r\n");
sprintf(banner3, "\e[37╩ \e[37┴ ┴┘\e[37└┘\e[37└─┘\e[37┴ ┴\e[37└─┘\e[37┴ ┴\e[37m\r\n");
I was just confused as I don't know what do \e[37 and \r\n mean. And can I change the colors?
This looks like an attempt to use ANSI terminal color escapes and Unicode box drawing characters to write the word "PANGAEA" in a large, stylized, colorful manner. I'm guessing it's part of a retro-style BBS or MUD system, intended to be interacted with over telnet or ssh. It doesn't work, because whoever wrote it made a bunch of mistakes. Here's a corrected, self-contained program:
#include <stdio.h>
int main(void)
{
printf("\e[31m╔═╗\e[32m┌─┐ \e[33m┌┐┌\e[34m┌─┐\e[35m┌─┐\e[36m┌─┐\e[37m┌─┐\e[0m\n");
printf("\e[31m╠═╝\e[32m├─┤ \e[33m│││\e[34m│ ┬\e[35m├─┤\e[36m├┤ \e[37m├─┤\e[0m\n");
printf("\e[31m╩ \e[32m┴ ┴┘\e[33m┘└┘\e[34m└─┘\e[35m┴ ┴\e[36m└─┘\e[37m┴ ┴\e[0m\n");
return 0;
}
The mistakes were: using \r\n instead of plain \n, leaving out the m at the end of each and every escape sequence, and a number of typos in the actual letters (missing spaces and the like).
I deliberately changed sprintf(bannerN, ... to printf to make it a self-contained program instead of a fragment of a larger system, and changed the actual color codes used for each letter to make it a more interesting demo. When I run this program on my computer I get this output:
The program will only work on your computer if your terminal emulator supports both ANSI color escapes and printing UTF-8 with no special ceremony. Most Unix-style operating systems nowadays support both by default; I don't know about Windows.

How do I make characters appear using getch()?

When I'm using getch() function and run my code, I don't see the characters that I'm inputting.
It is like they are hidden from what reason.
Does anyone know how can I make them appear while inputting them?
You need to use getche() or better _getche() instead of getch() to echo the input.
getch() and _getch() only fetch characters without displaying them to the console.
The e in getche() stands for echo.
From the Microsoft docs:
The _getche and _getwche functions read a single character from the console with echo, meaning that the character is displayed at the console.
Source: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getche-getwche?view=vs-2019
Furthermore, the use of getche() and getch() is deprecated by Microsoft. Use the versions with the preceding underscore instead:
The Microsoft-specific function name getche is a deprecated alias for the _getche function. By default, it generates Compiler warning (level 3) C4996. The name is deprecated because it doesn't follow the Standard C rules for implementation-specific names. However, the function is still supported.
Source: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getche?view=vs-2019
you should use function getche() ,it will display the character also.

How to make text colored in c lang in cmd?

how to make text colored in c lang?
The example is given in the image included.
This may be realised via ANSI escape sequences, although the effect depends on the terminal application used. For example to change the text to red use:
puts("\033[31m");
For further information visit Wikipedia, which has decent reference for all codes.

Bolding text in console output

For extra credit, the professor wants us to use bolding and/or underlining to text output in the current project.
The example he gave was b\bb o\bo l\bl d\bd is displayed as b o l d
Following that example, I marked up SPACE as
printf("\033[7mS\bSP\bPA\bAC\bCE\E- move forward one page\033[0m");
I'm also implementing reverse video by enclosing strings within \033[7m and \033[0m fields. The reverse video inverts the colors of the line appropriately, but doesn't seem to be affecting the bolding, since both strings with and without the reverse video are not bolding.
Could it be the standard shell used in Ubuntu 10.10 that is at fault?
I agree about using curses, but given your starting point ....
I think you want to use the 'bright' feature of VT100 for the bold, ESC[1m
You can probably find better doc on VT100 codes, but using this page I found the codes. ANSI/VT100 Escape Codes
I hope this helps.
Unless you're just trying to be masochistic, try using curses (or ncurses) instead.
// warning: Going from distant memory here...
curs_attron(A_INVERSE); // maybe A_REVERSE? I don't remember for sure.
curs_addstr("SPACE - move forward one page");
curs_attroff(A_INVERSE);

Print Arabic text in Vala

I tried
print ("السلام عليكم\n");
it outputs
?????? ?????
After looking at the generated c code
...
g_print ("السلام عليكم\n");
...
it appears that they're using g_print() which it is not doing that same as printf() in C which works perfectly fine with Arabic.
So, is there anyway to print arabic text in Vala?
Just add this to the start of your code:
Intl.setlocale (LocaleCategory.ALL, "");
By leaving the second parameter an empty string you're loading the LOCALE that the current user has set (which is likely to be a UTF-8 based one on modern Linux systems).
Windows is a different story here ...
See also:
https://valadoc.org/glib-2.0/GLib.Intl.setlocale.html
printing utf8 in glib
https://en.wikipedia.org/wiki/C_localization_functions
http://en.cppreference.com/w/c/locale/setlocale

Resources