Characters in C

char

In C, char values are stored in 1 byte, and are encoded as numbers using the ASCII encoding.

The man page for ascii lists all the encodings:

% man ascii

You should never use the ASCII numeric value directly, but you should know that the characters 'A' to 'Z' are contiguous, 'a' to 'z' are contiguous, and '0' to '9' are contigouous. Using the relationships between the upper case alphabetic, the lower case alphabetic, and the numeric characters I can write expressions like this:

  char ch;

  'a' + 2          // evaluates to the character value 'c' (becuase 'a' is
	                 // (encoded as a number we can add 2 to it to get 'c')
  'C' > 'A'     // evaluates to true, which is a non-zero value in C
                   // (0 is false)

  for(ch = 'a'; ch < 'z'; ch++) { ... 

  ch = getchar();  // read in a char value from stdin

  printf("ch is %c\n", ch);

C library functions for characters

The Standard C library has functions you can use for manipulating and testing character values:
#include <ctype.h>


int islower(ch);       
int isupper(ch);       // these functions return a non-zero value if the
int isalpha(ch);       // test is TRUE, otherwise they return 0 (FALSE) 
int isdigit(ch);
int isalnum(ch);       
int ispunct(ch);
int isspace(ch);       
char tolower(ch);
char toupper(ch);
Here are some examples of what they do/return:
char c = toupper('a');  // returns the character value 'A'

         islower('Z')   // returns 0 (FALSE) 

	 isspace(ch)    // returns non-zero (TRUE) if ch is a whitespace character
	                // ' ', '\t', '\n', '\r', '\f', or '\v' 
	 		
  • To get on-line documentation of C functions, use Unix's man utility:
    	% man isspace 
    
    Here is more information about using man.