Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect use of string storage in C and C++
    primarykey
    data
    text
    <p>Popular software developers and companies (<a href="http://www.joelonsoftware.com/articles/Unicode.html" rel="nofollow">Joel Spolsky, <strong>Fog Creek software</strong></a>) tend to use <strong>wchar_t</strong> for Unicode character <em>storage</em> when writing C or C++ code. When and how should one use <strong>char</strong> and <strong>wchar_t</strong> in respect to good coding practices?</p> <p>I am particularly interested in <strong>POSIX</strong> compliance when writing software that leverages Unicode.</p> <p>When using <strong>wchar_t</strong>, you can look up characters in an array of wide characters on a per-character or per-array-element basis:</p> <pre><code>/* C code fragment */ const wchar_t *overlord = L"ov€rlord"; if (overlord[2] == L'€') wprintf(L"Character comparison on a per-character basis.\n"); </code></pre> <p><em>How can you compare unicode bytes (or characters) when using <strong>char</strong>?</em></p> <p>So far my preferred way of comparing strings and characters of type <strong>char</strong> in C often looks like this:</p> <pre><code>/* C code fragment */ const char *mail[] = { "ov€rlord@masters.lt", "ov€rlord@masters.lt" }; if (mail[0][2] == mail[1][2] &amp;&amp; mail[0][3] == mail[1][3] &amp;&amp; mail[0][3] == mail[1][3]) printf("%s\n%zu", *mail, strlen(*mail)); </code></pre> <p>This method scans for the byte equivalent of a unicode character. The Unicode Euro symbol <strong>€</strong> takes up 3 bytes. Therefore one needs to compare three char array bytes to know if the Unicode characters match. Often you need to know the size of the character or string you want to compare and the bits it produces for the solution to work. This does not look like a good way of handling Unicode at all. <em>Is there a better way of comparing strings and character elements of type <strong>char</strong>?</em></p> <p>In addition, when using <strong>wchar_t</strong>, how can you scan the file contents to an array? The function <strong>fread</strong> does not seem to produce valid results.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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