C: Display special characters with printf()

21,476

Solution 1

I've find it,

#include <locale.h>
int main()
{
setlocale(LC_ALL,"");
 printf("%c%c%c%c\n", 'Î', 'Ç', ' È','Ì');
}

Thank you all for your awnsers it helps me a lot!!! :)

Solution 2

If your console is in UTF-8 it is possible just to print UTF-8 hex representation for your symbols. See similar answer for C++ Special Characters on Console

The following line prints heart:

printf("%c%c%c\n", '\xE2', '\x99', '\xA5');

However, since you print '\xCC', '\xC8', '\xCE','\xC7' and you have 4 different symbols it means that the console encoding is some kind of ASCII extension. Probably you have such encoding http://asciiset.com/. In that case you need characters '\x8c', 'x8d'. Unfortunately there are no capital version of those symbols in that encoding. So, you need some other encoding for your console, for example Latin-1, ISO/IEC 8859-1.


For Windows console:

UINT oldcp = GetConsoleOutputCP(); // save current console encoding

SetConsoleOutputCP(1252);
// print in cp1252 (Latin 1) encoding: each byte => one symbol
printf("%c%c%c%c\n", '\xCC', '\xC8', '\xCE','\xC7');

SetConsoleOutputCP(CP_UTF8);
// 3 hex bytes in UTF-8 => one 'heart' symbol
printf("%c%c%c\n", '\xE2', '\x99', '\xA5');

SetConsoleOutputCP(oldcp);

The console font should support Unicode (for example 'Lucida Console'). It can be changed manually in the console properties, since the default font may be 'Raster Fonts'.

Share:
21,476
MarceauC
Author by

MarceauC

Updated on July 09, 2022

Comments

  • MarceauC
    MarceauC almost 2 years

    I wanted to know how to display special characters with printf().
    I'm doing a string conversion program from Text to Code128 (barcode encoding).
    For this type of encoding I need to display characters such as Î, Ç, È, Ì.

    Example:
    string to convert: EPE196000100000002260500004N
    expected result: ÌEPEÇ3\ *R 6\ R $ÈNZÎ
    printf result typed: ╠EPEÇ3\ *R 6\ R $ÇNZ╬
    printf result image: [enter image description here]

    EDIT: I only can use C in this program no C++ at all. All the awnsers I've find so far are in C++ not C so I'm asking how to do it with C ^^