Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>char</code> is an 8-bit value capable of storing -128 &lt;= n &lt;= +127, frequently used to store character representations in different encodings and commonly - in Western, Roman-alphabet installations - <code>char</code> is used to indicate representation of ASCII or utf encoded values. 'Encoded' means the symbols/letter in the character set have been assigned numeric values. Think of the periodic table as an encoding of elements, so that 'H' (Hydrogen) is encoded as 1, Germanium as 32. In the ASCII (and UTF-8) tables, position 32 represents the character we call "space".</p> <p>When you use <code>operator &lt;&lt;</code> on a char value, the default behavior is to assume you are passing it a character encoding, e.g. an ASCII character code. If you do</p> <pre><code>char c = 'z'; char d = 122; char e = 0x7A; char f = '\x7a'; std::cout &lt;&lt; c &lt;&lt; d &lt;&lt; e &lt;&lt; f &lt;&lt; "\n"; </code></pre> <p>All four assignments are equivalent. 'z' is a shortcut/syntactic-sugar for <code>char(122)</code>, <code>0x7A</code> is hex for 122, and '\x7a' is an escape that forms the ascii character with a value of 0x7a or 122 - i.e. z.</p> <p>Where many new programmers go wrong is that they do this:</p> <pre><code>char n = 8; std::cout &lt;&lt; n &lt;&lt; endl; </code></pre> <p>this does not print "8", it prints ASCII character at position 8 in the ASCII table.</p> <p>Think for a moment:</p> <pre><code>char n = 8; // stores the value 8 char n = a; // what does this store? char n = '8'; // why is this different than the first line? </code></pre> <p>Lets rewind a moment: when you store <code>120</code> in a variable, it <strong>can</strong> represent the ASCII character <code>'x'</code>, but ultimately what is being stored is just the numeric value <code>120</code>, plain and simple.</p> <p>Specifically: When you pass <code>122</code> to a function that will ultimately use it to look up a font entry from a character set using the Latin1, ISO-8859-1, UTF-8 or similar encodings, then <code>120</code> means <code>'z'</code>.</p> <p>At the end of the day, <code>char</code> is just one of the standard integer value types, it can store values <code>-128 &lt;= n &lt;= +127</code>, it can trivially be promoted to a <code>short</code>, <code>int</code>, <code>long</code> or <code>long long</code>, etc, etc.</p> <p>While it is <em>generally</em> used to denote characters, it also frequently gets used as a way of saying "I'm only storing very small values" (such as integer percentages).</p> <pre><code>int incoming = 5000; int outgoing = 4000; char percent = char(outgoing * 100 / incoming); </code></pre> <p>If you want to print the numeric value, you simply need to promote it to a different value type:</p> <pre><code>std::cout &lt;&lt; (unsigned int)test &lt;&lt; "\n"; std::cout &lt;&lt; unsigned int(test) &lt;&lt; "\n"; </code></pre> <p>or the preferred C++ way</p> <pre><code>std::cout &lt;&lt; static_cast&lt;unsigned int&gt;(test) &lt;&lt; "\n"; </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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