Deepu
Deepu

Reputation: 7610

What is the need for signed and unsigned characters in C

What is the need for signed and unsigned characters in C?

Is there some special reason for having a signed and unsigned char in C? Or was it simply added for completeness so that the compiler does not have to check the data type before adding signed/unsigned modifier?

I am not asking about signed and unsigned variables. My doubt is about the special cases where an unsigned character variable will not be sufficient such that you have to depend on a signed character variable.

Upvotes: 0

Views: 1985

Answers (4)

Bo Persson
Bo Persson

Reputation: 92261

A char can be either signed or unsigned depending on what is most efficient for the underlying hardware. The keywords signed and unsigned allow you to explicitly specify that you want something else.

A quote from the C99 rationale:

Three types of char are specified: signed, plain, and unsigned. A plain char may be represented as either signed or unsigned depending upon the implementation, as in prior practice. The type signed char was introduced in C89 to make available a one-byte signed integer type on those systems which implemented plain char as unsigned char. For reasons of symmetry, the keyword signed is allowed as part of the type name of other integer types.

Upvotes: 3

meyumer
meyumer

Reputation: 5064

Information #1: char in C is just a small int, which uses 8 bits.

Information #2: Difference between signed and unsigned, is that one bit in the representation is used as the sign bit for a signed variable.

Information #3: As a result of (#2), signed variables hold different ranges (-128 to 127, in char case) compared to unsigned (0 to 255 in char case).

Q-A #1: why do we need unsigned?

In most cases (for instance representing a pointer) we do not need signed variables. By convention all locations in the memory are exposed to the program as a contiguous array of unsigned addresses.

Q-A #2: why do we need signed?

Generally, to do signed arithmetic.

Upvotes: 2

75inchpianist
75inchpianist

Reputation: 4102

I assume you are using a char to hold numbers, not characters.

So:

signed char gives you at least the -128 to 127 range. unsigned char gives you at least the 0 to 255 range.

A char is required by standard to be AT LEAST 8 bits, so that is the reason for my saying at least. It is possible for these values to be larger.

Anyway, to answer your question, having a char as unsigned frees the requirement for the first bit to be the 'sign' bit, thus allowing you to hold near double that of a signed char.

Upvotes: 1

DoxyLover
DoxyLover

Reputation: 3484

The thing you have to understand is that datatype "char" is actually just an integer, typically 8-bits wide. You can use it like any other inter datatype, assuming you respect the reduced value limits. There is no reason to limit "char" to characters.

On a 32/64-bit processor, there is typically no need to use such small integer fields, but on an 8-bit processor such as the 8051, 8-bit integers are no only much faster to process and use less (limited) memory.

Upvotes: 0

Related Questions