{"id":2593,"date":"2017-07-08T00:01:08","date_gmt":"2017-07-08T07:01:08","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2593"},"modified":"2017-07-15T07:42:27","modified_gmt":"2017-07-15T14:42:27","slug":"wide-characters-and-unicode-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2593","title":{"rendered":"Wide Characters and Unicode, Part III"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/06\/wide-characters.png\" alt=\"\" width=\"250\" height=\"66\" class=\"alignnone size-full wp-image-2575\" \/><\/p>\n<p>Programming is a fun hobby because once you unlock and open a door, you immediately find another locked door. Normal humans would experience frustration, but a good programmer enjoys a challenge and even sees the humor in the situation. A case in point is learning how to program wide characters in C: Once you think you&#8217;ve cleared a huge hurdle, you find another, taller one right behind it.<br \/>\n<!--more--><br \/>\nFor example, you think you understand wide characters and use the <em>putwchar()<\/em> function with ease. (See <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2578\">last week&#8217;s Lesson<\/a>.) Then you try using the <em>printf()<\/em> equivalent, <em>wprintf()<\/em>, and you&#8217;re back to square one.<\/p>\n<p>Don&#8217;t worry, I&#8217;ve been there, too.<\/p>\n<p>The issue with the <em>wprintf()<\/em> function, as well as the wide-character input function <em>wscanf()<\/em>, is that all the text in the function must be specified as wide characters. Unless you work with wide characters all the time, the solution isn&#8217;t apparent, and the error message doesn&#8217;t help.<\/p>\n<blockquote><p>Just tell me the solution!<\/p><\/blockquote>\n<p>You must specify wide-character strings and wide-character placeholders to make the wide-character input and output functions work. A wide-character string is prefixed by the letter L:<\/p>\n<p><code>L\"I am a wide string\"<\/code><\/p>\n<p>Prefixing an <code>L<\/code> (for <em>long<\/em>) before a quoted string converts it into unsigned integer values &mdash; wide characters.<\/p>\n<p>The placeholder for a wide character is <code>%lc<\/code>, with a lowercase L before lowercase C. This placeholder is used in both the <em>wprintf()<\/em> and <em>wscanf()<\/em> functions.<\/p>\n<pre class=\"screen\">\r\n#include &lt;locale.h&gt;\r\n#include &lt;wchar.h&gt;\r\n\r\nint main()\r\n{\r\n    wchar_t suits[4] = {\r\n        0x2660, 0x2665, 0x2663, 0x2666\r\n    };\r\n    wchar_t s;\r\n    int x;\r\n\r\n    setlocale(LC_CTYPE,\"UTF-8\");\r\n\r\n    printf(\"Enter suit: ( \");\r\n    for(x=0;x&lt;4;x++)\r\n    {\r\n        wprintf(L\"%lc \",suits[x]);\r\n    }\r\n    printf(\"): \");\r\n\r\n    wscanf(L\"%lc\",&s);\r\n    wprintf(L\"Suit set to %lc\\n\",s);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>wprintf()<\/em> function at Line 17 uses the formatting string <code>\"%lc\"<\/code> to output the wide characters stored in array <code>suits[]<\/code>. The array is defined at Line 6 as the type <em>wchar_t<\/em>, wide characters. The formatting string must be composed of wide-characters, so it&#8217;s prefixed with an <code>L<\/code>.<\/p>\n<p>The <em>wscanf()<\/em> function at Line 21 reads input for a wide character, though you can also type plain ASCII text. Again, the input format string is prefixed with an <code>L<\/code>, and the <code>%lc<\/code> placeholder is used. Input variable <code>s<\/code> is of the <em>wchar_t<\/em> type.<\/p>\n<p>Finally, Line 22 should make sense by now, with the <em>wprintf()<\/em> function using various wide-character doodads.<\/p>\n<p>The sample run:<\/p>\n<pre><code>Enter suit: ( &#9824; &#9829; &#9827; &#9830; ): &#9824;\r\nSuit set to &#9824;<\/code><\/pre>\n<p>Above, I copied and pasted the spade character because the keyboard lacks a spade character key and I don&#8217;t know any other ways to type a wide character in a terminal window.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2605\">next week&#8217;s Lesson<\/a>, I cover wide-character input functions beyond <em>wscanf()<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wide character, long character, long string. This Unicode stuff is full of surprises. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2593\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2593","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2593"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2593\/revisions"}],"predecessor-version":[{"id":2619,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2593\/revisions\/2619"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}