Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to write a standard C version of the <code>wc</code> utility that respects the current language setting when it is run, then you can indeed use the <code>wchar_t</code> versions of the stdio functions. At program startup, you should call <code>setlocale()</code>:</p> <pre><code>setlocale(LC_CTYPE, ""); </code></pre> <p>This will cause the wide character functions to use the appropriate character set defined by the environment - eg. on Unix-like systems, the <code>LANG</code> environment variable. For example, this means that if your <code>LANG</code> variable is set to a <code>UTF8</code> locale, the wide character functions will handle input and output in UTF8. (This is how the POSIX <code>wc</code> utility is specified to work).</p> <p>You can then use the wide-character versions of all the standard functions. For example, if you have code like this:</p> <pre><code>long words = 0; int in_word = 0; int c; while ((c = getchar()) != EOF) { if (isspace(c)) { if (in_word) { in_word = 0; words++; } } else { in_word = 1; } } </code></pre> <p>...you would convert it to the wide character version by changing <code>c</code> to a <code>wint_t</code>, <code>getchar()</code> to <code>getwchar()</code>, <code>EOF</code> to <code>WEOF</code> and <code>isspace()</code> to <code>iswspace()</code>:</p> <pre><code>long words = 0; int in_word = 0; wint_t c; while ((c = getwchar()) != WEOF) { if (iswspace(c)) { if (in_word) { in_word = 0; words++; } } else { in_word = 1; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload