<p>In C/C++, what an <code>unsigned char</code> is used for? How is it different from a regular <code>char</code>?</p>

[{'answer_id': 75238, 'author': 'Todd Gamblin', 'author_id': 9122, 'author_profile': 'https://Stackoverflow.com/users/9122', 'pm_score': 7, 'selected': False, 'text': '<p>This is implementation dependent, as the C standard does NOT define the signed-ness of <code>char</code>.  Depending on the platform, char may be <code>signed</code> or <code>unsigned</code>, so you need to explicitly ask for <code>signed char</code> or <code>unsigned char</code> if your implementation depends on it.  Just use <code>char</code> if you intend to represent characters from strings, as this will match what your platform puts in the string.</p>\n\n<p>The difference between <code>signed char</code> and <code>unsigned char</code> is as you\'d expect.  On most platforms, <code>signed char</code> will be an 8-bit two\'s complement number ranging from <code>-128</code> to <code>127</code>, and <code>unsigned char</code> will be an 8-bit unsigned integer (<code>0</code> to <code>255</code>).  Note the standard does NOT require that <code>char</code> types have 8 bits, only that <code>sizeof(char)</code> return <code>1</code>.  You can get at the number of bits in a char with <code>CHAR_BIT</code> in <code>limits.h</code>.  There are few if any platforms today where this will be something other than <code>8</code>, though.</p>\n\n<p>There is a nice summary of this issue <a href="http://www.arm.linux.org.uk/docs/faqs/signedchar.php" rel="noreferrer">here</a>.</p>\n\n<p>As others have mentioned since I posted this, you\'re better off using <code>int8_t</code> and <code>uint8_t</code> if you really want to represent small integers.</p>\n'}, {'answer_id': 75279, 'author': 'James Hopkin', 'author_id': 11828, 'author_profile': 'https://Stackoverflow.com/users/11828', 'pm_score': 4, 'selected': False, 'text': "<p><code>signed char</code> has range -128 to 127; <code>unsigned char</code> has range 0 to 255.</p>\n\n<p><code>char</code> will be equivalent to either signed char or unsigned char, depending on the compiler, but is a distinct type.</p>\n\n<p>If you're using C-style strings, just use <code>char</code>. If you need to use chars for arithmetic (pretty rare), specify signed or unsigned explicitly for portability.</p>\n"}, {'answer_id': 75324, 'author': 'neuroguy123', 'author_id': 12529, 'author_profile': 'https://Stackoverflow.com/users/12529', 'pm_score': 2, 'selected': False, 'text': '<p>Some googling found <a href="http://bytes.com/forum/thread61480.html" rel="nofollow noreferrer">this</a>, where people had a discussion about this.</p>\n\n<p>An unsigned char is basically a single byte.  So, you would use this if you need one byte of data (for example, maybe you want to use it to set flags on and off to be passed to a function, as is often done in the Windows API).</p>\n'}, {'answer_id': 75327, 'author': 'Zac Gochenour', 'author_id': 1812999, 'author_profile': 'https://Stackoverflow.com/users/1812999', 'pm_score': 3, 'selected': False, 'text': '<p>An <code>unsigned char</code> is an unsigned byte value (0 to 255).  You may be thinking of <code>char</code> in terms of being a "character" but it is really a numerical value.  The regular <code>char</code> is signed, so you have 128 values, and these values map to characters using ASCII encoding.  But in either case, what you are storing in memory is a byte value.</p>\n'}, {'answer_id': 75334, 'author': 'Julienne Walker', 'author_id': 13259, 'author_profile': 'https://Stackoverflow.com/users/13259', 'pm_score': 3, 'selected': False, 'text': "<p>In terms of direct values a regular char is used when the values are known to be between <code>CHAR_MIN</code> and <code>CHAR_MAX</code> while an unsigned char provides double the range on the positive end. For example, if <code>CHAR_BIT</code> is 8, the range of regular <code>char</code> is only guaranteed to be [0, 127] (because it can be signed or unsigned) while <code>unsigned char</code> will be [0, 255] and <code>signed char</code> will be [-127, 127].</p>\n\n<p>In terms of what it's used for, the standards allow objects of POD (plain old data) to be directly converted to an array of unsigned char. This allows you to examine the representation and bit patterns of the object. The same guarantee of safe type punning doesn't exist for char or signed char.</p>\n"}, {'answer_id': 75348, 'author': 'Dark Shikari', 'author_id': 11206, 'author_profile': 'https://Stackoverflow.com/users/11206', 'pm_score': 3, 'selected': False, 'text': "<p>If you like using various types of specific length and signedness, you're probably better off with <code>uint8_t</code>, <code>int8_t</code>, <code>uint16_t</code>, etc simply because they do exactly what they say.</p>\n"}, {'answer_id': 75364, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>An unsigned char uses the bit that is reserved for the sign of a regular char as another number. This changes the range to [0 - 255] as opposed to [-128 - 127]. </p>\n\n<p>Generally unsigned chars are used when you don't want a sign. This will make a difference when doing things like shifting bits (shift extends the sign) and other things when dealing with a char as a byte rather than using it as a number.</p>\n"}, {'answer_id': 75883, 'author': 'ugasoft', 'author_id': 10120, 'author_profile': 'https://Stackoverflow.com/users/10120', 'pm_score': 3, 'selected': False, 'text': "<p><code>unsigned char</code> is the heart of all bit trickery. In almost <em>all</em> compilers for <em>all</em> platforms an <code>unsigned char</code> is simply a <strong>byte</strong> and an unsigned integer of (usually) 8 bits that can be treated as a small integer or a pack of bits.</p>\n<p>In addition, as someone else has said, the standard doesn't define the sign of a char. So you have 3 distinct <code>char</code> types: <code>char</code>, <code>signed char</code>, <code>unsigned char</code>.</p>\n"}, {'answer_id': 79405, 'author': 'Zachary Garrett', 'author_id': 14692, 'author_profile': 'https://Stackoverflow.com/users/14692', 'pm_score': 5, 'selected': False, 'text': "<p>As for example usages of <em>unsigned char</em>:</p>\n\n<p><code>unsigned char</code> is often used in computer graphics, which very often (though not always) assigns a single byte to each colour component. It is common to see an RGB (or RGBA) colour represented as 24 (or 32) bits, each an <code>unsigned char</code>. Since <code>unsigned char</code> values fall in the range [0,255], the values are typically interpreted as: </p>\n\n<ul>\n<li>0 meaning a total lack of a given colour component.</li>\n<li>255 meaning 100% of a given colour pigment.</li>\n</ul>\n\n<p>So you would end up with RGB red as (255,0,0) -> (100% red, 0% green, 0% blue).</p>\n\n<p>Why not use a <code>signed char</code>? Arithmetic and bit shifting becomes problematic. As explained already, a <code>signed char</code>'s range is essentially shifted by -128. A very simple and naive (mostly unused) method for converting RGB to grayscale is to average all three colour components, but this runs into problems when the values of the colour components are negative. Red (255, 0, 0) averages to (85, 85, 85) when using <code>unsigned char</code> arithmetic. However, if the values were <code>signed char</code>s (127,-128,-128), we would end up with (-99, -99, -99), which would be (29, 29, 29) in our <code>unsigned char</code> space, which is incorrect.</p>\n"}, {'answer_id': 80376, 'author': 'bk1e', 'author_id': 8090, 'author_profile': 'https://Stackoverflow.com/users/8090', 'pm_score': 3, 'selected': False, 'text': '<p><code>char</code> and <code>unsigned char</code> aren\'t guaranteed to be 8-bit types on all platforms&mdash;they are guaranteed to be 8-bit or larger. Some platforms have <a href="https://isocpp.org/wiki/faq/intrinsic-types#bits-per-byte" rel="nofollow noreferrer">9-bit, 32-bit, or 64-bit bytes</a>. However, the most common platforms today (Windows, Mac, Linux x86, etc.) have 8-bit bytes.</p>\n'}, {'answer_id': 87648, 'author': 'Fruny', 'author_id': 16815, 'author_profile': 'https://Stackoverflow.com/users/16815', 'pm_score': 10, 'selected': True, 'text': "<p>In C++, there are three <em>distinct</em> character types:</p>\n<ul>\n<li><code>char</code></li>\n<li><code>signed char</code></li>\n<li><code>unsigned char</code></li>\n</ul>\n<p>If you are using character types for <em>text</em>, use the unqualified <code>char</code>:</p>\n<ul>\n<li>it is the type of character literals like <code>'a'</code> or <code>'0'</code> (in C++ only, in C their type is <code>int</code>)</li>\n<li>it is the type that makes up C strings like <code>&quot;abcde&quot;</code></li>\n</ul>\n<p>It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through inequalities - although if you limit yourself to ASCII (0-127) you're just about safe.</p>\n<p>If you are using character types as <em>numbers</em>, use:</p>\n<ul>\n<li><code>signed char</code>, which gives you <em>at least</em> the -127 to 127 range. (-128 to 127 is common)</li>\n<li><code>unsigned char</code>, which gives you <em>at least</em> the 0 to 255 range.</li>\n</ul>\n<p>&quot;At least&quot;, because the C++ standard only gives the minimum range of values that each numeric type is required to cover. <code>sizeof (char)</code> is required to be 1 (i.e. one byte), but a byte could in theory be for example 32 bits. <strong><code>sizeof</code> would still be report its size as <code>1</code></strong> - meaning that you <em>could</em> have <code>sizeof (char) == sizeof (long) == 1</code>.</p>\n"}, {'answer_id': 442647, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 5, 'selected': False, 'text': "<p>Because I feel it's really called for, I just want to state some rules of C and C++ (they are the same in this regard). First, <em>all bits</em> of <code>unsigned char</code> participate in determining the value if any unsigned char object. Second, <code>unsigned char</code> is explicitly stated unsigned.</p>\n<p>Now, I had a discussion with someone about what happens when you convert the value <code>-1</code> of type int to <code>unsigned char</code>. He refused the idea that the resulting <code>unsigned char</code> has all its bits set to 1, because he was worried about sign representation. But he didn't have to be. It's immediately following out of this rule that the conversion does what is intended:</p>\n<blockquote>\n<p>If the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. (<code>6.3.1.3p2</code> in a C99 draft)</p>\n</blockquote>\n<p>That's a mathematical description. C++ describes it in terms of modulo calculus, which yields to the same rule. Anyway, what is <em>not</em> guaranteed is that all bits in the integer <code>-1</code> are one before the conversion. So, what do we have so we can claim that the resulting <code>unsigned char</code> has all its <code>CHAR_BIT</code> bits turned to 1?</p>\n<ol>\n<li>All bits participate in determining its value - that is, no padding bits occur in the object.</li>\n<li>Adding only one time <code>UCHAR_MAX+1</code> to <code>-1</code> will yield a value in range, namely <code>UCHAR_MAX</code></li>\n</ol>\n<p>That's enough, actually! So whenever you want to have an <code>unsigned char</code> having all its bits one, you do</p>\n<pre><code>unsigned char c = (unsigned char)-1;\n</code></pre>\n<p>It also follows that a conversion is <em>not</em> just truncating higher order bits. The fortunate event for <em>two's complement</em> is that it is just a truncation there, but the same isn't necessarily true for other sign representations.</p>\n"}, {'answer_id': 14456663, 'author': 'munna', 'author_id': 1865289, 'author_profile': 'https://Stackoverflow.com/users/1865289', 'pm_score': 3, 'selected': False, 'text': '<p><code>unsigned char</code> takes only positive values....like <strong>0</strong> to <strong>255</strong></p>\n\n<p>where as</p>\n\n<p><code>signed char</code> takes both positive and negative values....like <strong>-128</strong> to <strong>+127</strong></p>\n'}, {'answer_id': 45228423, 'author': 'ZhaoGang', 'author_id': 2830167, 'author_profile': 'https://Stackoverflow.com/users/2830167', 'pm_score': 2, 'selected': False, 'text': '<p>quoted frome "the c programming laugage" book:</p>\n\n<p>The qualifier <code>signed</code> or <code>unsigned</code> may be applied to char or any integer. unsigned numbers\nare always positive or zero, and obey the laws of arithmetic modulo 2^n, where n is the number\nof bits in the type. So, for instance, if chars are 8 bits, unsigned char variables have values\nbetween 0 and 255, while signed chars have values between -128 and 127 (in a two\' s\ncomplement machine.) Whether plain chars are signed or unsigned is machine-dependent,\nbut printable characters are always positive.</p>\n'}, {'answer_id': 47480841, 'author': 'NL628', 'author_id': 8925851, 'author_profile': 'https://Stackoverflow.com/users/8925851', 'pm_score': 2, 'selected': False, 'text': '<p><code>unsigned char</code> takes only positive values: 0 to 255 while\n<code>signed char</code> takes positive and negative values: -128 to +127.</p>\n'}, {'answer_id': 59587899, 'author': 'Kalana', 'author_id': 11383441, 'author_profile': 'https://Stackoverflow.com/users/11383441', 'pm_score': 2, 'selected': False, 'text': '<p><code>signed char</code> and <code>unsigned char</code> both represent 1byte, but they have different ranges.</p>\n\n<pre><code>   Type        |      range\n-------------------------------\nsigned char    |  -128 to +127\nunsigned char  |     0 to 255\n</code></pre>\n\n<p>In <code>signed char</code> if we consider <code>char letter = \'A\'</code>, \'A\' is represent binary of 65 in <code>ASCII/Unicode</code>, If 65 can be stored, -65 also can be stored. There are no negative binary values in <code>ASCII/Unicode</code> there for no need to worry about negative values.</p>\n\n<p><strong>Example</strong></p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint main()\n{\n    signed char char1 = 255;\n    signed char char2 = -128;\n    unsigned char char3 = 255;\n    unsigned char char4 = -128;\n\n    printf("Signed char(255) : %d\\n",char1);\n    printf("Unsigned char(255) : %d\\n",char3);\n\n    printf("\\nSigned char(-128) : %d\\n",char2);\n    printf("Unsigned char(-128) : %d\\n",char4);\n\n    return 0;\n}\n</code></pre>\n\n<p><strong>Output -:</strong></p>\n\n<pre><code>Signed char(255) : -1\nUnsigned char(255) : 255\n\nSigned char(-128) : -128\nUnsigned char(-128) : 128\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/75191', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1785/']