What are signed and unsigned chars?

  • Thread starter adjacent
  • Start date
  • Tags
    C++
In summary, the conversation was about the data type char in C++, specifically the differences between signed and unsigned char. The book mentioned that an 8-bit unsigned char can hold values from 0 to 255, while a signed char can hold values from -127 to 127. The conversation also included a discussion about ASCII characters and code pages. It was mentioned that understanding the differences between signed and unsigned chars may be important for optimizing software performance. The conversation also touched on the fact that characters are stored as numbers on a computer and can be represented in different forms, such as decimal or hexadecimal. In conclusion, the conversation highlighted the importance of understanding computer basics before learning to program in C++.
  • #1
adjacent
Gold Member
1,552
63
I am reading a book called C++ primer, which mentions about a data type of C++ called char(Character). I know that this data type can hold characters such as "A" or "5". But what does signed and unsigned char mean?
The book says: "an 8-bit unsigned char can hold values from 0 through 255 inclusive". What does values mean here?
I have Googled but found no good explanation. The book also does not explain this. :eek:
 
Technology news on Phys.org
  • #2
signed chars are 7-bit but stored as 8-bit (ie 1 byte). They are the official ASCII character set:

http://en.wikipedia.org/wiki/ASCII

the unsigned chars are the ones added by PC vendors like IBM and MS. As an example, box drawing characters used in the day when graphics wasnt an option for computer displays (ie characters could only be displayed) as an extension to ASCII.

http://en.wikipedia.org/wiki/Code_page_437

Code pages may vary from country to country where glyphs relevant to the country overlay others.
 
  • Like
Likes adjacent
  • #3
Oh, thank you very much. Do I have to know a lot about unsigned and signed chars to learn C++? I am not a computer student, so I don't know much about computers yet.
 
  • #5
jtbell said:
A Google search for "C++ unsigned char" gave me this as its first hit:

http://stackoverflow.com/questions/75191/what-is-an-unsigned-char

It's from 2008, so the situation may have changed somewhat in newer versions of C++.
Yes, I read that. The part which confused me was this part:
  • 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."
What does values mean here? I am guessing that each character is represented by a specific value, like in that code page given above by jedishrfu.
 
  • #6
When using c or c++ for embedded or systems programming the signedness and bit size of integers becomes very important (and confusing when using 'char' and 'int') during the manipulation of bit pattern sequences (possible values within the range of the variable purely from its bit size) with logical/arithmetic operations that also evaluate sign from those bit patterns so a large 'in range' unsigned 8-bit 'uint8_t' number could be a small signed 8-bit 'int8_t' number of the same bits. So there is usually a header that defines standard types.
http://embeddedgurus.com/stack-over...t-c-tips-1-choosing-the-correct-integer-size/

There are esoteric reasons for the precise use of signedness that are mainly concerned with optimizing software performance.
http://clip.dia.fi.upm.es/~jorge/docs/wrapped-intervals-aplas12.pdf
 
Last edited:
  • #7
8 bits can store a set of binary bit patterns ranging from 00000000 to 11111111. What those patterns mean as integers depends on whether the data type is specified as 'signed char' or 'unsigned char'. Try the following program:

Code:
#include <iostream>
using namespace std;

int main ()
{
//  hexadecimal 61 = binary 01000001
    unsigned char my_unsigned_char = 0x61;
    signed char my_signed_char = 0x61;

// display them as ints
    cout << "Unsigned: " << int(my_unsigned_char) << endl;
    cout << "Signed: " << int(my_signed_char) << endl;

// hexadecimal E1 = binary 11000001
    my_unsigned_char = 0xE1;
    my_signed_char = 0xE1;

// display them as ints
    cout << "Unsigned: " << int(my_unsigned_char) << endl;
    cout << "Signed: " << int(my_signed_char) << endl;

    return 0;
}
 
  • Like
Likes adjacent
  • #8
Most computers, which includes all mainstream computers, operate with binary digits (bits), which are grouped into bytes (8 bits), words (16 bits), double words (32 bits) and quad-words (64 bits). These entities can be used in various ways, but very frequently they are used to represent integers. Integers, in turn, can be signed or unsigned.

In C and C++, 'char' is an integer of the smallest width, but no less than 8 bits; in most practical cases it is exactly 8 bits, i.e., a byte. Because it is an integer, it can be signed or unsigned. One byte can represent 256 different integer values; for signed characters, they are -128 ... 127, for unsigned characters, they are 0 ... 255.

These values are in turn re-interpreted as symbols. For example, the value 65, which is the same for both signed and unsigned, is interpreted as symbol 'A'.

If the char type is used only to store characters (i.e., symbols), then the difference between the signed and unsigned char types is practically absent. But in certain cases, char types are used for arithmetic, in which case the difference may be crucial. An example of such arithmetic is the Base-64 encoding.
 
Last edited:
  • Like
Likes adjacent and nsaspook
  • #9
adjacent said:
What does values mean here? I am guessing that each character is represented by a specific value, like in that code page given above by jedishrfu.
The only things that can be stored on a computer are numbers, which means that characters are stored as numbers. For example, the character 'A' is stored as a bit pattern 0100 0001 in binary, or 65 as a decimal number. In C you can use printf to display a character in various forms.
Code:
char ch = 'A';
printf("Decimal: %d\n", ch);  // Prints 65, the decimal form
printf("Hex:%x\n", ch);  // Prints 0x41, a hexadecimal form
printf("Octal: %o\n", ch); // Prints 0101, an octal form
printf("Character: %c\n", ch) // Prints A, the character form
The underlying value of the variable ch is 0100 0001. We get four different representations in the code above because printf is converting from the bit pattern to representations in other bases or other forms.
 
  • Like
Likes adjacent
  • #10
Thanks a lot everyone, I think I should learn a bit about computers before learning to program in C++. I am glad that I chose to learn C++.
 

1. What are signed and unsigned chars?

Signed and unsigned chars are data types used in computer programming languages to represent characters or small integers. They are typically 8 bits in size and are used to store a single character or integer value.

2. What is the difference between signed and unsigned chars?

The main difference between signed and unsigned chars is how they interpret and store data. Signed chars can store positive and negative values, while unsigned chars can only store positive values. This is because the first bit in a signed char is used to represent the sign of the value, whereas in an unsigned char, all bits are used to represent the value.

3. How are signed and unsigned chars used in programming?

Signed and unsigned chars are commonly used in programming for tasks such as storing and manipulating character data, representing small integers, and performing bitwise operations. They are also used in data structures, such as strings and arrays, to store and access individual characters or bytes of data.

4. Can signed and unsigned chars be converted to each other?

Yes, signed and unsigned chars can be converted to each other using type casting. This involves explicitly specifying the data type when assigning a value to a variable. However, this conversion should be done carefully to avoid data loss or unexpected results, as the range of values that can be represented by signed and unsigned chars differ.

5. Are signed and unsigned chars still commonly used in modern programming?

Yes, signed and unsigned chars are still commonly used in modern programming, particularly in low-level languages such as C and C++. They are also used in higher-level languages, such as Java and Python, although they may be abstracted away from the programmer. In some cases, other data types, such as strings or integers, may be used instead of chars for certain tasks, but signed and unsigned chars still have a variety of uses in programming.

Similar threads

  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
1
Views
790
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
11
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
Back
Top