Thread: What's the point of unsigned char?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    87

    What's the point of unsigned char?

    In C++ (and C too), char as is should be used to represent the ASCII characters, and the reason one wishes to use the unsigned char and signed char is if the purpose is to use small integral types (-128 to 127 and 0 - 255 range, respectively where a char is ~ 1B).

    So trivial code below, is why doesn't it output an integer but that I needed to typecast it, but doesn't this defeat purpose of trying to use a small int since a short is ~ 2B, so what's the benefit of unsigned/signed char then?

    Code:
    int main()
    {
       unsigned char x = 65;//what is this for, if it doesn't print a small int (1B) but 'A' instead
        
        cout << short(x) << endl;//If I have to typecast it, the short (smallest integer type) than it's going to take up 2B so defeats purpose of using a small int for whatever reason...
       return 0;
    }
    And we should use int8_t and uint8_t to avoid ambiguity for portability, just as we should use size_t, time_t, is that correct? But my question is not this, but why we need to typecast to output its integer value since a short (smallest integer type of 2B) needs to be used but a char is only 1B so we don't save space to use a char to represent small integers. I hope I am clear what I mean...
    Last edited by monkey_c_monkey; 07-13-2012 at 06:29 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Chars are typically used as characters in C++. Therefore, cout automatically assumes that you want to print a character, and not a number.
    But fret not, since a variable typically takes up space, your variable may take up as little as one byte (depending on OS, compiler, architecture, etc). However, when you type cast it, it will most likely end up in a cpu register until the function call to cout is over and then thrown away, wasting no memory.
    Regardless, using char for storing small ranges is just not necessary on today's PCs (embedded is another matter).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I see two different issues here: the usage of one-byte types and the importance of signedness.

    > char as is should be used to represent the ASCII characters
    There are many reasons why one would use a one-byte type without ever needing to print it as a character and typecast, such as bitwise operations, efficiency, network parsing, et cetera. Char certainly can be used to store characters, but it is often used for other applications, especially arrays where (intuitively) each element is one byte.

    > so what's the benefit of unsigned/signed char then?
    Each variation of char has its own applications. Also, besides the obvious increase in capacity, unsigned operations are slightly faster.

    > but a char is only 1B so we don't save space to use a char to represent small integers,
    There's no sense trying to save three bytes, especially when you take padding and alignment into account. Besides, using int is usually the best choice, as it's the native word size, meaning basically that it's the fastest size that your particular machine can manipulate.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    An unsigned char can hold any value from 0 to 255, and if you assign it the value 222 say, then it has the value 222, regardless of whatever some other function decides to do with that. Who cares what 'cout' does with an unsigned char by default.
    Sometimes people use the value an unsigned char holds to representing characters. Sometimes people use it to represent shades of say red in an RGB colour space. Sometimes people use it for a boolean flag.

    If you want it to print as a decimal value, as a programmer you should be perfectly capabale of making that happen. If casting is how you can achieve that, great - casting it is. Most of the time when people use an unsigned char to hold as integral value, printing that value out as test is the last thing on their mind.

    What's the benefit?! An 8-bit type take up half as much space as a 16-bit type, or a quarter that of a 32-bit type. Say you need 10 million of them, which one is going to take up less space? You're a smart enough person; do the math.

    You'll have to forgive my bluntness. Replying with the aparent attitude shown here is merely to help get the point across. I'm not actually feeling this way right now, but it seemed like a good idea anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  2. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM
  3. invalid conversion from `unsigned char*' to `char*'
    By tux88 in forum C Programming
    Replies: 6
    Last Post: 10-14-2007, 12:25 AM
  4. cast unsigned char* to (the default) signed char*
    By Mario F. in forum C++ Programming
    Replies: 24
    Last Post: 07-27-2007, 10:41 AM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM