1

since C language using the char as integer internally(correspondent ASCII is stored). for internal calculation we can use signed and unsigned char.

other than that, any other use??

Shyju C R
  • 11
  • 5
  • I hope you find your answer [here](https://stackoverflow.com/questions/4337217/difference-between-signed-unsigned-char) – Outlaw May 22 '18 at 05:54
  • 1
    One example: An unsigned char pointer is allowed to alias other types and thereby it can be used to access the bit pattern of the other type. – Support Ukraine May 22 '18 at 05:58
  • @Outlaw : that i saw, any other diffrents? – Shyju C R May 22 '18 at 06:02
  • ASCII is a common character representation but other have existed around like [EBCDIC](https://en.wikipedia.org/wiki/EBCDIC) for example – Serge Ballesta May 22 '18 at 06:14
  • @SergeBallesta [link](https://communities.sas.com/t5/SAS-Procedures/ASCII-to-EBCDIC-problems-with-ebcdic-format/td-p/102461) – Shyju C R May 22 '18 at 06:50
  • even though ASCII is not enough to represent the char in other language like Chinese. because char is more. UNICODE is better – Shyju C R May 22 '18 at 06:51
  • @ShyjuCR: Simply in most implementations, `char` is only 8 bits large so cannot represents all unicode characters. That's the reason why ASCII and other 8 bits charset like the ISO-8859-xx family are still used and why unicode can be encoded (as multi-byte) in UTF-8. – Serge Ballesta May 22 '18 at 07:56
  • Possible duplicate of [What is the need for signed and unsigned characters in C](https://stackoverflow.com/questions/15416586/what-is-the-need-for-signed-and-unsigned-characters-in-c) – Bo Persson May 22 '18 at 08:54

1 Answers1

0

signed and unsigned char are first and foremost just small integers. Do you need to store a large quantity of small numbers (in the range [-127, +127]¹ or [0, 255])? You can use an array of signed or unsigned chars and save memory compared to pretty much any other type. That's what is done for e.g. images - a grayscale image is generally stored as an array of unsigned char (and an RGB image is generally stored as an array of 3 unsigned char components).

The second usage of char is for character strings, which you probably already saw; notice that char is a distinct type from both signed char and unsigned char, and its signedness is implementation defined. This is stupid and inconvenient in many situations - and leads to sad stuff such as the mandatory cast to unsigned char when calling functions of the toupper/isupper family.

Finally, char & co. are defined as the "underlying storage" of the C abstract machine. sizeof(char) == 1 by definition, and any type can be aliased through a (signed|unsigned)? char pointer to access its underlying bit representation.


  1. Yes, -127; [-127, +127] is the minimum range allowed for signed char by the standard, as it still allows sign and magnitude representation; more realistic, on any real-world machine of this century it will be at least [-128, 127].
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • i have an answer for your page [status](https://stackoverflow.com/users/214671/matteo-italia), size_t is big enough to hold amount of data. but in case of int and double their max size is already defined by the compiler(32bit->unsigned int, 64->unsigned long long ). – Shyju C R May 22 '18 at 06:40
  • @ShyjuCR that doesn't make any sense. `size_t` also has a maximum value – phuclv May 22 '18 at 07:03
  • @ShyjuCR: er... I don't get exactly what you're saying, but no. – Matteo Italia May 22 '18 at 07:36