Abstract

The char type is the smallest numeric type, but is most commonly used to hold character values.

Form

char-type
char | signed char | char signed

Semantics

char-type
  1. The size of the char type is not specified exactly by the language specification; it is specified as no larger than the short type, and large enough to hold a character of the execution character set.
  2. Typically, for PCs, both 16 bit and 32 bit compilers assign 1 byte (8 bits) to char type variables.
  3. The language specification does not specify an encoding, but most current computers use 2's complement encoding for char integers:
             
    765 43210
    S 2's Complement Magnitude
    The leftmost bit is the sign bit (1 for negative numbers, 0 for positive numbers and zero), and the other 7 bits are the magnitude.  For non-negative numbers, the magnitude is directly in natural binary.  For negative numbers, the magnitude is the 2's complement of the encoded representation.
  4. An 8 bit 2's complement number has a range of -27 to (27 -1), or - 128 to 127.
  5. For any computer, the range of the char type can be found by using the symbolic constants SCHAR_MIN and SCHAR_MAX, usually defined in the limits.h header file.  (

Remarks

Example

#include <stdio.h>
#include <limits.h> 

int main () {
  char a;                  /* signed char by default */
  signed char i;           /* Explicit               */
  char signed x;           /* Explicit               */
  
  a = 73;
  i = 74;
  x = 75;
  
  printf ("a is %c (%d) (%d byte)\n", a, a, sizeof a);
  printf ("i is %c (%d) (%d byte)\n", i, i, sizeof i);
  printf ("x is %c (%d) (%d byte)\n", x, x, sizeof x);
  
  a = -a;
  i = -i;
  x = -x;

  printf ("a is %c (%d) (%d byte)\n", a, a, sizeof a);
  printf ("i is %c (%d) (%d byte)\n", i, i, sizeof i);
  printf ("x is %c (%d) (%d byte)\n", x, x, sizeof x);

  printf ("The range of the char type is from %d to %d\n",
          CHAR_MIN, CHAR_MAX);
  printf ("The range of the signed char type is from %d to %d\n",
          SCHAR_MIN, SCHAR_MAX);
          
  return 0;
}

Output