The Three char Types

From RAD Studio
Jump to: navigation, search

Go Up to Character Constants Overview Index

In C, single-character constants, such as 'A', '\t', or '007', are represented as int values. In this case, the low-order byte is sign extended into the high bit; that is, if the value is greater than 127 (base 10), the upper bit is set to -1 (=0xFF). This can be disabled by declaring that the default char type is unsigned.

The three character types, char, signed char, and unsigned char, require an 8-bit (one byte) storage. By default, the compiler treats character declarations as signed. Use the -K compiler option to treat character declarations as unsigned. The behavior of C programs is unaffected by the distinction between the three character types.

In a C++ program, a function can be overloaded with arguments of type char, signed char, or unsigned char. For example, the following function prototypes are valid and distinct:

void func(char ch);
void func(signed char ch);
void func(unsigned char ch);

If only one of the above prototypes exists, it will accept any of the three character types. For example, the following is acceptable:

void func(unsigned char ch);
void main(void)
{
  signed char ch = 'x';
  func(ch);
}

See Also