In C++, there are three distinct character types:

  1. char
  2. signed char
  3. unsigned char

1. char

If you are using character types for text, use the unqualified char:

  • it is the type of character literals like 'a' or '0' (in C++ only, in C their type is int)
  • it is the type that makes up C strings like "abcde"

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.

2. signed char/ 3. unsigned char

If you are using character types as numbers, use:

  • signed char, which gives you at least the -127 to 127 range. (-128 to 127 is common)
  • unsigned char, which gives you at least the 0 to 255 range. This might be useful for displaying an octet e.g. as hex value.

"At least", because the C++ standard only gives the minimum range of values that each numeric type is required to cover. sizeof (char) is required to be 1 (i.e. one byte), but a byte could in theory be for example 32 bits. sizeof would still be report its size as 1 - meaning that you could have sizeof (char) == sizeof (long) == 1.

Answer from Fruny on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @mazen.elheni โ€บ unsigned-char-in-c-with-examples-efa3db909c71
Unsigned char in C with Examples. char is the most basic data type in Cโ€ฆ | by Mazen Elheni | Medium
February 5, 2025 - char is the most basic data type ... almost all compilers. ... unsigned char is a character datatype where the variable consumes all the 8 bits of the memory and there is no sign bit (which is there in signed char)....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ unsigned-char-in-c-with-examples
unsigned char in C with Examples - GeeksforGeeks
July 12, 2025 - Now character datatype can be divided into 2 types: ... unsigned char unsigned char is a character datatype where the variable consumes all the 8 bits of the memory and there is no sign bit (which is there in signed char).
Discussions

c++ - What is an unsigned char? - Stack Overflow
In C/C++, what is an unsigned char used for? How is it different from a regular char? More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Difference between signed / unsigned char - Stack Overflow
Closed 8 years ago. So I know that the difference between a signed int and unsigned int is that a bit is used to signify if the number if positive or negative, but how does this apply to a char? More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the use of unsigned char?
There are 3 character types: signed char unsigned char char (yes, this is indeed a distinct type, not "alias", but for practical purposes it is the same as one of the above two, implementation specified) sizeof for these is defined to be 1. Using sizeof char is always redundant, though it can be used like a comment, making code more clear. On any platform you are likely to ever work on, char is 8 bits, and synonym to "byte". Therefore, int8_t is probably signed char and uint8_t is unsigned char typedefs. More on reddit.com
๐ŸŒ r/cpp_questions
39
9
April 20, 2023
c++ - Char and unsigned char - Software Engineering Stack Exchange
Where is unsigned char used in C ( please tell about some real-world examples )? Why would we need both char and unsigned char? Char ranges from -127 to 128 ( 8-bit integer ) Unsigned Char ranges ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
May 22, 2013
Top answer
1 of 16
629

In C++, there are three distinct character types:

  1. char
  2. signed char
  3. unsigned char

1. char

If you are using character types for text, use the unqualified char:

  • it is the type of character literals like 'a' or '0' (in C++ only, in C their type is int)
  • it is the type that makes up C strings like "abcde"

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.

2. signed char/ 3. unsigned char

If you are using character types as numbers, use:

  • signed char, which gives you at least the -127 to 127 range. (-128 to 127 is common)
  • unsigned char, which gives you at least the 0 to 255 range. This might be useful for displaying an octet e.g. as hex value.

"At least", because the C++ standard only gives the minimum range of values that each numeric type is required to cover. sizeof (char) is required to be 1 (i.e. one byte), but a byte could in theory be for example 32 bits. sizeof would still be report its size as 1 - meaning that you could have sizeof (char) == sizeof (long) == 1.

2 of 16
106

This is implementation dependent, as the C standard does NOT define the signed-ness of char. Depending on the platform, char may be signed or unsigned, so you need to explicitly ask for signed char or unsigned char if your implementation depends on it. Just use char if you intend to represent characters from strings, as this will match what your platform puts in the string.

The difference between signed char and unsigned char is as you'd expect. On most platforms, signed char will be an 8-bit two's complement number ranging from -128 to 127, and unsigned char will be an 8-bit unsigned integer (0 to 255). Note the standard does NOT require that char types have 8 bits, only that sizeof(char) return 1. You can get at the number of bits in a char with CHAR_BIT in limits.h. There are few if any platforms today where this will be something other than 8, though.

There is a nice summary of this issue here.

As others have mentioned since I posted this, you're better off using int8_t and uint8_t if you really want to represent small integers.

Top answer
1 of 3
6

Where is unsigned char used in C ( please tell about some real-world examples )? Why would we need both char and unsigned char?

signed or unsigned are properties of the different C data types (char, short, int, etc...). So it's not a question of whether we need both, both come automatically as part of how C is defined.

Char ranges from -127 to 128 ( 8-bit integer ) Unsigned Char ranges from 0 to 255 ( 8-bit integer )

While that's true for most of the platforms out there, there's nothing garanting that char will be signed. In fact on ARM platforms, it's unsigned. See this fix: http://www.spinics.net/lists/linux-btrfs/msg20653.html as an example of real-world bug introduced by assuming that char is signed.

If the purpose is to store ASCII, why would we need both?

The thing is, the purpose is not to store ASCII. It happens to be used to store ASCII but it's a not a necessity.

char a = 127;

unsigned char b = 255;

When I print it using std::cout. It gives me different characters. Can you explain why? ( I'm using Microsoft vs11 compiler

I think what you're looking for is the following:

#include <iostream>

int main(void)
{
        char a = -1; 
        unsigned char b = 255;
        std::cout << a << std::endl << b << std::endl;
        return 0;
}

That is '-1' signed will be equal to '255' unsigned. Please note that this is heavily implementation dependent, and there's nothing that guarantees it will work accross all platforms and compilers.

2 of 3
7

unsigned char stores a number. You can use it when you need to represent numbers in the range 0 to 255 and want to conserve memory.

Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ analytics-vidhya โ€บ what-is-char-signed-char-unsigned-char-and-character-literals-in-c-796034139b98
What is char , signed char , unsigned char , and character literals in C? | by mohamad wael | Analytics Vidhya | Medium
February 26, 2024 - The unsigned char type can only store nonnegative integer values , it has a minimum range between 0 and 127 , as defined by the C standard. The signed char type can store , negative , zero , and positive integer values .
๐ŸŒ
IncludeHelp
includehelp.com โ€บ code-snippets โ€บ unsigned-char-declaration-read-and-print-in-c-language.aspx
'unsigned char' - Declaration, Assign and Usage in C ...
May 5, 2018 - char is a data type in C programming language which can store value from -128 to +127. It generally used to store character values. unsigned is a qualifier which is used to increase the values to be written in the memory blocks.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ unsigned-char-c
How to Use unsigned char in C With Examples โ€“ Linux Hint
In C programming, the unsigned char data type is a useful option when dealing with dynamic values. Unlike short data or integers, unsigned char uses all 8 bits of its memory and has no signed bits. This means that unsigned data ranges from 0 to 255, allowing larger values โ€‹โ€‹to be stored ...
๐ŸŒ
SEI CERT
wiki.sei.cmu.edu โ€บ confluence โ€บ display โ€บ c โ€บ INT07-C.+Use+only+explicitly+signed+or+unsigned+char+type+for+numeric+values
INT07-C. Use only explicitly signed or unsigned char type for numeric values | CERT Secure Coding
In this noncompliant code example, the char -type variable c may be signed or unsigned. Assuming 8-bit, two's complement character types, this code may print out either i/c = 5 (unsigned) or i/c = -17 (signed).
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2181227 โ€บ char-and-unsigned-char-in-c
char and unsigned char in C
February 24, 2020 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
IME USP
ime.usp.br โ€บ ~pf โ€บ algorithms โ€บ chapters โ€บ int-and-char.html
What is a signed int? an unsigned int? a char?
In C language, the natural numbers are known as unsigned integers, while the integer numbers are known as signed integers. Unsigned integers are implemented by the data types unsigned char and unsigned int. Signed integers are implemented by the data types char and int.
๐ŸŒ
SEI CERT
wiki.sei.cmu.edu โ€บ confluence โ€บ display โ€บ c โ€บ STR34-C.+Cast+characters+to+unsigned+char+before+converting+to+larger+integer+sizes
STR34-C. Cast characters to unsigned char before converting to larger integer sizes | CERT Secure Coding
This example, however, violates STR04-C. Use plain char for characters in the basic character set . In this compliant solution, the result of the expression *c_str++ is cast to unsigned char before assignment to the int variable c :
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ C_data_types
C data types - Wikipedia
5 days ago - Because the data model defines how different programs communicate, a uniform data model is used within a given operating system application interface. In practice, char is usually 8 bits in size and short is usually 16 bits in size (as are their unsigned counterparts).
๐ŸŒ
GNU
gnu.org โ€บ software โ€บ c-intro-and-ref โ€บ manual โ€บ html_node โ€บ Signed-and-Unsigned-Types.html
Signed and Unsigned Types (GNU C Language Manual)
An unsigned integer type can represent only positive numbers and zero. A signed type can represent both positive and negative numbers, in a range spread almost equally on both sides of zero. For instance, unsigned char holds numbers from 0 to 255 (on most computers), while signed char holds ...
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 149746-whats-point-unsigned-char.html
What's the point of unsigned char?
July 13, 2012 - 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. ... 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 ...
๐ŸŒ
Quora
quora.com โ€บ When-should-we-use-unsigned-char-versus-char-in-C-language
When should we use unsigned char versus char in C language? - Quora
Answer (1 of 9): Any time you donโ€™t expect an integral value to be less than zero, it is appropriate to declare it unsigned to document that fact. In C, many functions take int arguments that are never negative. You should declare as int anything you want to pass to a function taking an int argu...
๐ŸŒ
Quora
quora.com โ€บ What-is-a-signed-and-unsigned-char-in-C
What is a signed and unsigned char in C? - Quora
Unsigned char has range from 0 to 255 and can represent only +ve values. Signed char has range from -128 to 127. Left most bit is reserved to store either 1 or 0 to represent -ve or +ve sign while the remaining 7 bits are used to represent the ...