Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing out a unicode character with printf

Tags:

c

printf

unicode

I am trying to print out a Ș by passing in its corresponding decimal value into printf. The output is nothing at all. Why doesn't my code work?

#include <stdio.h>
int main()
{
    printf("%lc",536);
    return 0;
}
like image 780
Mike Avatar asked Jan 22 '17 23:01

Mike


People also ask

How do I print a character in printf?

Generally, printf() function is used to print the text along with the values. If you want to print % as a string or text, you will have to use '%%'. Neither single % will print anything nor it will show any error or warning.

How do I print Unicode?

We can put the Unicode value with the prefix \u. Thus we can successfully print the Unicode character.

Which function is used to print the Unicode value of a character?

The ord function in python accepts a single character as an argument and returns an integer value representing the Unicode equivalent of that character.

Can you use Unicode in C?

It can represent all 1,114,112 Unicode characters. Most C code that deals with strings on a byte-by-byte basis still works, since UTF-8 is fully compatible with 7-bit ASCII. Characters usually require fewer than four bytes. String sort order is preserved.


1 Answers

On macOS Sierra 10.12.2 with GCC 6.3.0, if I run this program (compiled from mb37.c into mb37):

#include <locale.h>
#include <stdio.h>
#include <wchar.h>      /* wint_t */

int main(void)
{
    setlocale(LC_ALL, "");
    printf("%lc\n", (wint_t)536);
    return 0;
}

the output is:

$ ./mb37
Ș
$

That, I believe, is the desired output. If the setlocale() line is removed, then no output at all is produced — not even a newline. The locale used is en_US.UTF-8; my terminal handles UTF-8 too. The locale name is found by capturing and printing the return value from setlocale() — a regular string.

The wint_t cast is semi-optional; it so happens that a 64-bit compilation without the cast or the <wchar.h> header also produces the same output, but there is a mild coincidence that wint_t is the same as int. That takes some tracking; wint_t is defined as __darwin_wint_t which is defined as __darwin_ct_rune_t which is defined as int. To be portably correct, the cast is necessary. On some systems, it may not be necessary (and macOS Sierra is one such system).

The newline in the printf() is not 100% necessary, but if it is omitted, the next prompt immediately follows the U+0218 LATIN CAPITAL LETTER S WITH COMMA BELOW. It is better to ensure that the output ends with a newline.

like image 132
Jonathan Leffler Avatar answered Nov 01 '22 07:11

Jonathan Leffler